blob: 4d106c4fa48ff1f15bf2c23d974c0050d98e5f48 [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 Brazdil3e187382015-06-26 09:59:52 +0000845void LocationsBuilderX86::VisitGoto(HGoto* got) {
846 got->SetLocations(nullptr);
847}
848
849void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
850 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100851 DCHECK(!successor->IsExitBlock());
852
853 HBasicBlock* block = got->GetBlock();
854 HInstruction* previous = got->GetPrevious();
855
856 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000857 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100858 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
859 return;
860 }
861
862 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
863 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
864 }
865 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000866 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000867 }
868}
869
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000870void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000871 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000872}
873
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000874void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700875 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000876}
877
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700878void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
879 Label* true_target,
880 Label* false_target,
881 Label* always_true_target) {
882 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100883 if (cond->IsIntConstant()) {
884 // Constant condition, statically compared against 1.
885 int32_t cond_value = cond->AsIntConstant()->GetValue();
886 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700887 if (always_true_target != nullptr) {
888 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100889 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100890 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100891 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100892 DCHECK_EQ(cond_value, 0);
893 }
894 } else {
895 bool materialized =
896 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
897 // Moves do not affect the eflags register, so if the condition is
898 // evaluated just before the if, we don't need to evaluate it
899 // again.
900 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700901 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100902 if (materialized) {
903 if (!eflags_set) {
904 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700905 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100906 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500907 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100908 } else {
909 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
910 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700911 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100912 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700913 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100914 }
915 } else {
916 Location lhs = cond->GetLocations()->InAt(0);
917 Location rhs = cond->GetLocations()->InAt(1);
918 // LHS is guaranteed to be in a register (see
919 // LocationsBuilderX86::VisitCondition).
920 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000921 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100922 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +0100923 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500924 if (constant == 0) {
925 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
926 } else {
927 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
928 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100929 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000930 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100931 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700932 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700933 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100934 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700935 if (false_target != nullptr) {
936 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000937 }
938}
939
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700940void LocationsBuilderX86::VisitIf(HIf* if_instr) {
941 LocationSummary* locations =
942 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
943 HInstruction* cond = if_instr->InputAt(0);
944 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
945 locations->SetInAt(0, Location::Any());
946 }
947}
948
949void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
950 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
951 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
952 Label* always_true_target = true_target;
953 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
954 if_instr->IfTrueSuccessor())) {
955 always_true_target = nullptr;
956 }
957 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
958 if_instr->IfFalseSuccessor())) {
959 false_target = nullptr;
960 }
961 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
962}
963
964void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
965 LocationSummary* locations = new (GetGraph()->GetArena())
966 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
967 HInstruction* cond = deoptimize->InputAt(0);
968 DCHECK(cond->IsCondition());
969 if (cond->AsCondition()->NeedsMaterialization()) {
970 locations->SetInAt(0, Location::Any());
971 }
972}
973
974void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
975 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
976 DeoptimizationSlowPathX86(deoptimize);
977 codegen_->AddSlowPath(slow_path);
978 Label* slow_path_entry = slow_path->GetEntryLabel();
979 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
980}
981
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000982void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000983 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000984}
985
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000986void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
987 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000988}
989
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000990void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100991 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000992}
993
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000994void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100995 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700996 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000997}
998
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100999void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001000 LocationSummary* locations =
1001 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002 switch (store->InputAt(1)->GetType()) {
1003 case Primitive::kPrimBoolean:
1004 case Primitive::kPrimByte:
1005 case Primitive::kPrimChar:
1006 case Primitive::kPrimShort:
1007 case Primitive::kPrimInt:
1008 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001009 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001010 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1011 break;
1012
1013 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001014 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001015 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1016 break;
1017
1018 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001019 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001020 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001021}
1022
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001023void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001024 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001025}
1026
Roland Levillain0d37cd02015-05-27 16:39:19 +01001027void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001028 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001029 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001030 locations->SetInAt(0, Location::RequiresRegister());
1031 locations->SetInAt(1, Location::Any());
Roland Levillain0d37cd02015-05-27 16:39:19 +01001032 if (cond->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -05001033 // We need a byte register.
1034 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001035 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001036}
1037
Roland Levillain0d37cd02015-05-27 16:39:19 +01001038void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
1039 if (cond->NeedsMaterialization()) {
1040 LocationSummary* locations = cond->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001041 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001042 // Clear register: setcc only sets the low byte.
1043 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -05001044 Location lhs = locations->InAt(0);
1045 Location rhs = locations->InAt(1);
1046 if (rhs.IsRegister()) {
1047 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1048 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -08001049 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001050 if (constant == 0) {
1051 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1052 } else {
1053 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1054 }
Dave Allison20dfc792014-06-16 20:44:29 -07001055 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001056 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001057 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001058 __ setb(X86Condition(cond->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001059 }
Dave Allison20dfc792014-06-16 20:44:29 -07001060}
1061
1062void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1063 VisitCondition(comp);
1064}
1065
1066void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1067 VisitCondition(comp);
1068}
1069
1070void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1071 VisitCondition(comp);
1072}
1073
1074void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1075 VisitCondition(comp);
1076}
1077
1078void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1079 VisitCondition(comp);
1080}
1081
1082void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1083 VisitCondition(comp);
1084}
1085
1086void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1087 VisitCondition(comp);
1088}
1089
1090void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1091 VisitCondition(comp);
1092}
1093
1094void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1095 VisitCondition(comp);
1096}
1097
1098void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1099 VisitCondition(comp);
1100}
1101
1102void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1103 VisitCondition(comp);
1104}
1105
1106void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1107 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001108}
1109
1110void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001111 LocationSummary* locations =
1112 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001113 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001114}
1115
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001116void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001117 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001118 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001119}
1120
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001121void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1122 LocationSummary* locations =
1123 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1124 locations->SetOut(Location::ConstantLocation(constant));
1125}
1126
1127void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1128 // Will be generated at use site.
1129 UNUSED(constant);
1130}
1131
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001132void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001133 LocationSummary* locations =
1134 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001135 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001136}
1137
1138void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1139 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001140 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001141}
1142
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001143void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1144 LocationSummary* locations =
1145 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1146 locations->SetOut(Location::ConstantLocation(constant));
1147}
1148
1149void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1150 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001151 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001152}
1153
1154void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1155 LocationSummary* locations =
1156 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1157 locations->SetOut(Location::ConstantLocation(constant));
1158}
1159
1160void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1161 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001162 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001163}
1164
Calin Juravle27df7582015-04-17 19:12:31 +01001165void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1166 memory_barrier->SetLocations(nullptr);
1167}
1168
1169void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1170 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1171}
1172
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001173void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001174 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001175}
1176
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001177void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001178 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001179 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001180}
1181
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001182void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001183 LocationSummary* locations =
1184 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001185 switch (ret->InputAt(0)->GetType()) {
1186 case Primitive::kPrimBoolean:
1187 case Primitive::kPrimByte:
1188 case Primitive::kPrimChar:
1189 case Primitive::kPrimShort:
1190 case Primitive::kPrimInt:
1191 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001192 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001193 break;
1194
1195 case Primitive::kPrimLong:
1196 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001197 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001198 break;
1199
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001200 case Primitive::kPrimFloat:
1201 case Primitive::kPrimDouble:
1202 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001203 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001204 break;
1205
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001206 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001207 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001208 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001209}
1210
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001211void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001212 if (kIsDebugBuild) {
1213 switch (ret->InputAt(0)->GetType()) {
1214 case Primitive::kPrimBoolean:
1215 case Primitive::kPrimByte:
1216 case Primitive::kPrimChar:
1217 case Primitive::kPrimShort:
1218 case Primitive::kPrimInt:
1219 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001220 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001221 break;
1222
1223 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001224 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1225 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001226 break;
1227
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001228 case Primitive::kPrimFloat:
1229 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001230 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001231 break;
1232
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001233 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001234 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001235 }
1236 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001237 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001238}
1239
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001240void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001241 // When we do not run baseline, explicit clinit checks triggered by static
1242 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1243 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001244
Mark Mendellfb8d2792015-03-31 22:16:59 -04001245 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001246 if (intrinsic.TryDispatch(invoke)) {
1247 return;
1248 }
1249
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001250 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001251
1252 if (codegen_->IsBaseline()) {
1253 // Baseline does not have enough registers if the current method also
1254 // needs a register. We therefore do not require a register for it, and let
1255 // the code generation of the invoke handle it.
1256 LocationSummary* locations = invoke->GetLocations();
1257 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1258 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1259 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1260 }
1261 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001262}
1263
Mark Mendell09ed1a32015-03-25 08:30:06 -04001264static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1265 if (invoke->GetLocations()->Intrinsified()) {
1266 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1267 intrinsic.Dispatch(invoke);
1268 return true;
1269 }
1270 return false;
1271}
1272
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001273void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001274 // When we do not run baseline, explicit clinit checks triggered by static
1275 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1276 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001277
Mark Mendell09ed1a32015-03-25 08:30:06 -04001278 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1279 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001280 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001281
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001282 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001283 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001284 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001285 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001286}
1287
1288void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1289 HandleInvoke(invoke);
1290}
1291
1292void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001293 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001294 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001295}
1296
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001297void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001298 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001299 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1300 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001301 LocationSummary* locations = invoke->GetLocations();
1302 Location receiver = locations->InAt(0);
1303 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001304 DCHECK(receiver.IsRegister());
1305 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001306 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001307 // temp = temp->GetMethodAt(method_offset);
1308 __ movl(temp, Address(temp, method_offset));
1309 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001310 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001311 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001312
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001313 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001314 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001315}
1316
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001317void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1318 HandleInvoke(invoke);
1319 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001320 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001321}
1322
1323void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1324 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001325 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001326 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1327 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001328 LocationSummary* locations = invoke->GetLocations();
1329 Location receiver = locations->InAt(0);
1330 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1331
1332 // Set the hidden argument.
1333 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001334 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001335
1336 // temp = object->GetClass();
1337 if (receiver.IsStackSlot()) {
1338 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1339 __ movl(temp, Address(temp, class_offset));
1340 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001341 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001342 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001343 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001344 // temp = temp->GetImtEntryAt(method_offset);
1345 __ movl(temp, Address(temp, method_offset));
1346 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001347 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001348 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001349
1350 DCHECK(!codegen_->IsLeafMethod());
1351 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1352}
1353
Roland Levillain88cb1752014-10-20 16:36:47 +01001354void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1355 LocationSummary* locations =
1356 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1357 switch (neg->GetResultType()) {
1358 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001359 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001360 locations->SetInAt(0, Location::RequiresRegister());
1361 locations->SetOut(Location::SameAsFirstInput());
1362 break;
1363
Roland Levillain88cb1752014-10-20 16:36:47 +01001364 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001365 locations->SetInAt(0, Location::RequiresFpuRegister());
1366 locations->SetOut(Location::SameAsFirstInput());
1367 locations->AddTemp(Location::RequiresRegister());
1368 locations->AddTemp(Location::RequiresFpuRegister());
1369 break;
1370
Roland Levillain88cb1752014-10-20 16:36:47 +01001371 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001372 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001373 locations->SetOut(Location::SameAsFirstInput());
1374 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001375 break;
1376
1377 default:
1378 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1379 }
1380}
1381
1382void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1383 LocationSummary* locations = neg->GetLocations();
1384 Location out = locations->Out();
1385 Location in = locations->InAt(0);
1386 switch (neg->GetResultType()) {
1387 case Primitive::kPrimInt:
1388 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001389 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001390 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001391 break;
1392
1393 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001394 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001395 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001396 __ negl(out.AsRegisterPairLow<Register>());
1397 // Negation is similar to subtraction from zero. The least
1398 // significant byte triggers a borrow when it is different from
1399 // zero; to take it into account, add 1 to the most significant
1400 // byte if the carry flag (CF) is set to 1 after the first NEGL
1401 // operation.
1402 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1403 __ negl(out.AsRegisterPairHigh<Register>());
1404 break;
1405
Roland Levillain5368c212014-11-27 15:03:41 +00001406 case Primitive::kPrimFloat: {
1407 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001408 Register constant = locations->GetTemp(0).AsRegister<Register>();
1409 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001410 // Implement float negation with an exclusive or with value
1411 // 0x80000000 (mask for bit 31, representing the sign of a
1412 // single-precision floating-point number).
1413 __ movl(constant, Immediate(INT32_C(0x80000000)));
1414 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001415 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001416 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001417 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001418
Roland Levillain5368c212014-11-27 15:03:41 +00001419 case Primitive::kPrimDouble: {
1420 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001421 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001422 // Implement double negation with an exclusive or with value
1423 // 0x8000000000000000 (mask for bit 63, representing the sign of
1424 // a double-precision floating-point number).
1425 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001426 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001427 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001428 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001429
1430 default:
1431 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1432 }
1433}
1434
Roland Levillaindff1f282014-11-05 14:15:05 +00001435void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001436 Primitive::Type result_type = conversion->GetResultType();
1437 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001438 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001439
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001440 // The float-to-long and double-to-long type conversions rely on a
1441 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001442 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001443 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1444 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001445 ? LocationSummary::kCall
1446 : LocationSummary::kNoCall;
1447 LocationSummary* locations =
1448 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1449
David Brazdilb2bd1c52015-03-25 11:17:37 +00001450 // The Java language does not allow treating boolean as an integral type but
1451 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001452
Roland Levillaindff1f282014-11-05 14:15:05 +00001453 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001454 case Primitive::kPrimByte:
1455 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001456 case Primitive::kPrimBoolean:
1457 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001458 case Primitive::kPrimShort:
1459 case Primitive::kPrimInt:
1460 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001461 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001462 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1463 // Make the output overlap to please the register allocator. This greatly simplifies
1464 // the validation of the linear scan implementation
1465 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001466 break;
1467
1468 default:
1469 LOG(FATAL) << "Unexpected type conversion from " << input_type
1470 << " to " << result_type;
1471 }
1472 break;
1473
Roland Levillain01a8d712014-11-14 16:27:39 +00001474 case Primitive::kPrimShort:
1475 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001476 case Primitive::kPrimBoolean:
1477 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001478 case Primitive::kPrimByte:
1479 case Primitive::kPrimInt:
1480 case Primitive::kPrimChar:
1481 // Processing a Dex `int-to-short' instruction.
1482 locations->SetInAt(0, Location::Any());
1483 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1484 break;
1485
1486 default:
1487 LOG(FATAL) << "Unexpected type conversion from " << input_type
1488 << " to " << result_type;
1489 }
1490 break;
1491
Roland Levillain946e1432014-11-11 17:35:19 +00001492 case Primitive::kPrimInt:
1493 switch (input_type) {
1494 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001495 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001496 locations->SetInAt(0, Location::Any());
1497 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1498 break;
1499
1500 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001501 // Processing a Dex `float-to-int' instruction.
1502 locations->SetInAt(0, Location::RequiresFpuRegister());
1503 locations->SetOut(Location::RequiresRegister());
1504 locations->AddTemp(Location::RequiresFpuRegister());
1505 break;
1506
Roland Levillain946e1432014-11-11 17:35:19 +00001507 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001508 // Processing a Dex `double-to-int' instruction.
1509 locations->SetInAt(0, Location::RequiresFpuRegister());
1510 locations->SetOut(Location::RequiresRegister());
1511 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001512 break;
1513
1514 default:
1515 LOG(FATAL) << "Unexpected type conversion from " << input_type
1516 << " to " << result_type;
1517 }
1518 break;
1519
Roland Levillaindff1f282014-11-05 14:15:05 +00001520 case Primitive::kPrimLong:
1521 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001522 case Primitive::kPrimBoolean:
1523 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001524 case Primitive::kPrimByte:
1525 case Primitive::kPrimShort:
1526 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001527 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001528 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001529 locations->SetInAt(0, Location::RegisterLocation(EAX));
1530 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1531 break;
1532
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001533 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001534 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001535 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001536 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001537 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1538 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1539
Vladimir Marko949c91f2015-01-27 10:48:44 +00001540 // The runtime helper puts the result in EAX, EDX.
1541 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001542 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001543 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001544
1545 default:
1546 LOG(FATAL) << "Unexpected type conversion from " << input_type
1547 << " to " << result_type;
1548 }
1549 break;
1550
Roland Levillain981e4542014-11-14 11:47:14 +00001551 case Primitive::kPrimChar:
1552 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001553 case Primitive::kPrimBoolean:
1554 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001555 case Primitive::kPrimByte:
1556 case Primitive::kPrimShort:
1557 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001558 // Processing a Dex `int-to-char' instruction.
1559 locations->SetInAt(0, Location::Any());
1560 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1561 break;
1562
1563 default:
1564 LOG(FATAL) << "Unexpected type conversion from " << input_type
1565 << " to " << result_type;
1566 }
1567 break;
1568
Roland Levillaindff1f282014-11-05 14:15:05 +00001569 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001570 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001571 case Primitive::kPrimBoolean:
1572 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001573 case Primitive::kPrimByte:
1574 case Primitive::kPrimShort:
1575 case Primitive::kPrimInt:
1576 case Primitive::kPrimChar:
1577 // Processing a Dex `int-to-float' instruction.
1578 locations->SetInAt(0, Location::RequiresRegister());
1579 locations->SetOut(Location::RequiresFpuRegister());
1580 break;
1581
1582 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001583 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001584 locations->SetInAt(0, Location::Any());
1585 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001586 break;
1587
Roland Levillaincff13742014-11-17 14:32:17 +00001588 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001589 // Processing a Dex `double-to-float' instruction.
1590 locations->SetInAt(0, Location::RequiresFpuRegister());
1591 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001592 break;
1593
1594 default:
1595 LOG(FATAL) << "Unexpected type conversion from " << input_type
1596 << " to " << result_type;
1597 };
1598 break;
1599
Roland Levillaindff1f282014-11-05 14:15:05 +00001600 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001601 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001602 case Primitive::kPrimBoolean:
1603 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001604 case Primitive::kPrimByte:
1605 case Primitive::kPrimShort:
1606 case Primitive::kPrimInt:
1607 case Primitive::kPrimChar:
1608 // Processing a Dex `int-to-double' instruction.
1609 locations->SetInAt(0, Location::RequiresRegister());
1610 locations->SetOut(Location::RequiresFpuRegister());
1611 break;
1612
1613 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001614 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001615 locations->SetInAt(0, Location::Any());
1616 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001617 break;
1618
Roland Levillaincff13742014-11-17 14:32:17 +00001619 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001620 // Processing a Dex `float-to-double' instruction.
1621 locations->SetInAt(0, Location::RequiresFpuRegister());
1622 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001623 break;
1624
1625 default:
1626 LOG(FATAL) << "Unexpected type conversion from " << input_type
1627 << " to " << result_type;
1628 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001629 break;
1630
1631 default:
1632 LOG(FATAL) << "Unexpected type conversion from " << input_type
1633 << " to " << result_type;
1634 }
1635}
1636
1637void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1638 LocationSummary* locations = conversion->GetLocations();
1639 Location out = locations->Out();
1640 Location in = locations->InAt(0);
1641 Primitive::Type result_type = conversion->GetResultType();
1642 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001643 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001644 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001645 case Primitive::kPrimByte:
1646 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001647 case Primitive::kPrimBoolean:
1648 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001649 case Primitive::kPrimShort:
1650 case Primitive::kPrimInt:
1651 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001652 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001653 if (in.IsRegister()) {
1654 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001655 } else {
1656 DCHECK(in.GetConstant()->IsIntConstant());
1657 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1658 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1659 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001660 break;
1661
1662 default:
1663 LOG(FATAL) << "Unexpected type conversion from " << input_type
1664 << " to " << result_type;
1665 }
1666 break;
1667
Roland Levillain01a8d712014-11-14 16:27:39 +00001668 case Primitive::kPrimShort:
1669 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001670 case Primitive::kPrimBoolean:
1671 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001672 case Primitive::kPrimByte:
1673 case Primitive::kPrimInt:
1674 case Primitive::kPrimChar:
1675 // Processing a Dex `int-to-short' instruction.
1676 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001677 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001678 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001679 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001680 } else {
1681 DCHECK(in.GetConstant()->IsIntConstant());
1682 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001683 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001684 }
1685 break;
1686
1687 default:
1688 LOG(FATAL) << "Unexpected type conversion from " << input_type
1689 << " to " << result_type;
1690 }
1691 break;
1692
Roland Levillain946e1432014-11-11 17:35:19 +00001693 case Primitive::kPrimInt:
1694 switch (input_type) {
1695 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001696 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001697 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001698 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001699 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001700 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001701 } else {
1702 DCHECK(in.IsConstant());
1703 DCHECK(in.GetConstant()->IsLongConstant());
1704 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001705 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001706 }
1707 break;
1708
Roland Levillain3f8f9362014-12-02 17:45:01 +00001709 case Primitive::kPrimFloat: {
1710 // Processing a Dex `float-to-int' instruction.
1711 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1712 Register output = out.AsRegister<Register>();
1713 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1714 Label done, nan;
1715
1716 __ movl(output, Immediate(kPrimIntMax));
1717 // temp = int-to-float(output)
1718 __ cvtsi2ss(temp, output);
1719 // if input >= temp goto done
1720 __ comiss(input, temp);
1721 __ j(kAboveEqual, &done);
1722 // if input == NaN goto nan
1723 __ j(kUnordered, &nan);
1724 // output = float-to-int-truncate(input)
1725 __ cvttss2si(output, input);
1726 __ jmp(&done);
1727 __ Bind(&nan);
1728 // output = 0
1729 __ xorl(output, output);
1730 __ Bind(&done);
1731 break;
1732 }
1733
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001734 case Primitive::kPrimDouble: {
1735 // Processing a Dex `double-to-int' instruction.
1736 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1737 Register output = out.AsRegister<Register>();
1738 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1739 Label done, nan;
1740
1741 __ movl(output, Immediate(kPrimIntMax));
1742 // temp = int-to-double(output)
1743 __ cvtsi2sd(temp, output);
1744 // if input >= temp goto done
1745 __ comisd(input, temp);
1746 __ j(kAboveEqual, &done);
1747 // if input == NaN goto nan
1748 __ j(kUnordered, &nan);
1749 // output = double-to-int-truncate(input)
1750 __ cvttsd2si(output, input);
1751 __ jmp(&done);
1752 __ Bind(&nan);
1753 // output = 0
1754 __ xorl(output, output);
1755 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001756 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001757 }
Roland Levillain946e1432014-11-11 17:35:19 +00001758
1759 default:
1760 LOG(FATAL) << "Unexpected type conversion from " << input_type
1761 << " to " << result_type;
1762 }
1763 break;
1764
Roland Levillaindff1f282014-11-05 14:15:05 +00001765 case Primitive::kPrimLong:
1766 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001767 case Primitive::kPrimBoolean:
1768 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001769 case Primitive::kPrimByte:
1770 case Primitive::kPrimShort:
1771 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001772 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001773 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001774 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1775 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001776 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001777 __ cdq();
1778 break;
1779
1780 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001781 // Processing a Dex `float-to-long' instruction.
1782 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001783 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1784 break;
1785
Roland Levillaindff1f282014-11-05 14:15:05 +00001786 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001787 // Processing a Dex `double-to-long' instruction.
1788 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1789 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001790 break;
1791
1792 default:
1793 LOG(FATAL) << "Unexpected type conversion from " << input_type
1794 << " to " << result_type;
1795 }
1796 break;
1797
Roland Levillain981e4542014-11-14 11:47:14 +00001798 case Primitive::kPrimChar:
1799 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001800 case Primitive::kPrimBoolean:
1801 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001802 case Primitive::kPrimByte:
1803 case Primitive::kPrimShort:
1804 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001805 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1806 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001807 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001808 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001809 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001810 } else {
1811 DCHECK(in.GetConstant()->IsIntConstant());
1812 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001813 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001814 }
1815 break;
1816
1817 default:
1818 LOG(FATAL) << "Unexpected type conversion from " << input_type
1819 << " to " << result_type;
1820 }
1821 break;
1822
Roland Levillaindff1f282014-11-05 14:15:05 +00001823 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001824 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001825 case Primitive::kPrimBoolean:
1826 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001827 case Primitive::kPrimByte:
1828 case Primitive::kPrimShort:
1829 case Primitive::kPrimInt:
1830 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001831 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001832 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001833 break;
1834
Roland Levillain6d0e4832014-11-27 18:31:21 +00001835 case Primitive::kPrimLong: {
1836 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001837 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00001838
Roland Levillain232ade02015-04-20 15:14:36 +01001839 // Create stack space for the call to
1840 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
1841 // TODO: enhance register allocator to ask for stack temporaries.
1842 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
1843 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1844 __ subl(ESP, Immediate(adjustment));
1845 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001846
Roland Levillain232ade02015-04-20 15:14:36 +01001847 // Load the value to the FP stack, using temporaries if needed.
1848 PushOntoFPStack(in, 0, adjustment, false, true);
1849
1850 if (out.IsStackSlot()) {
1851 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
1852 } else {
1853 __ fstps(Address(ESP, 0));
1854 Location stack_temp = Location::StackSlot(0);
1855 codegen_->Move32(out, stack_temp);
1856 }
1857
1858 // Remove the temporary stack space we allocated.
1859 if (adjustment != 0) {
1860 __ addl(ESP, Immediate(adjustment));
1861 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001862 break;
1863 }
1864
Roland Levillaincff13742014-11-17 14:32:17 +00001865 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001866 // Processing a Dex `double-to-float' instruction.
1867 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001868 break;
1869
1870 default:
1871 LOG(FATAL) << "Unexpected type conversion from " << input_type
1872 << " to " << result_type;
1873 };
1874 break;
1875
Roland Levillaindff1f282014-11-05 14:15:05 +00001876 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001877 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001878 case Primitive::kPrimBoolean:
1879 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001880 case Primitive::kPrimByte:
1881 case Primitive::kPrimShort:
1882 case Primitive::kPrimInt:
1883 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001884 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001885 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001886 break;
1887
Roland Levillain647b9ed2014-11-27 12:06:00 +00001888 case Primitive::kPrimLong: {
1889 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001890 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00001891
Roland Levillain232ade02015-04-20 15:14:36 +01001892 // Create stack space for the call to
1893 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
1894 // TODO: enhance register allocator to ask for stack temporaries.
1895 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
1896 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1897 __ subl(ESP, Immediate(adjustment));
1898 }
1899
1900 // Load the value to the FP stack, using temporaries if needed.
1901 PushOntoFPStack(in, 0, adjustment, false, true);
1902
1903 if (out.IsDoubleStackSlot()) {
1904 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
1905 } else {
1906 __ fstpl(Address(ESP, 0));
1907 Location stack_temp = Location::DoubleStackSlot(0);
1908 codegen_->Move64(out, stack_temp);
1909 }
1910
1911 // Remove the temporary stack space we allocated.
1912 if (adjustment != 0) {
1913 __ addl(ESP, Immediate(adjustment));
1914 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00001915 break;
1916 }
1917
Roland Levillaincff13742014-11-17 14:32:17 +00001918 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001919 // Processing a Dex `float-to-double' instruction.
1920 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001921 break;
1922
1923 default:
1924 LOG(FATAL) << "Unexpected type conversion from " << input_type
1925 << " to " << result_type;
1926 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001927 break;
1928
1929 default:
1930 LOG(FATAL) << "Unexpected type conversion from " << input_type
1931 << " to " << result_type;
1932 }
1933}
1934
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001935void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001936 LocationSummary* locations =
1937 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001938 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001939 case Primitive::kPrimInt: {
1940 locations->SetInAt(0, Location::RequiresRegister());
1941 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1942 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1943 break;
1944 }
1945
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001946 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001947 locations->SetInAt(0, Location::RequiresRegister());
1948 locations->SetInAt(1, Location::Any());
1949 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001950 break;
1951 }
1952
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001953 case Primitive::kPrimFloat:
1954 case Primitive::kPrimDouble: {
1955 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001956 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001957 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001958 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001959 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001960
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001961 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001962 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1963 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001964 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001965}
1966
1967void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1968 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001969 Location first = locations->InAt(0);
1970 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001971 Location out = locations->Out();
1972
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001973 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001974 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001975 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001976 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1977 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04001978 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
1979 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05001980 } else {
1981 __ leal(out.AsRegister<Register>(), Address(
1982 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1983 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001984 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001985 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1986 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1987 __ addl(out.AsRegister<Register>(), Immediate(value));
1988 } else {
1989 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1990 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001991 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001992 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001993 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001994 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001995 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001996 }
1997
1998 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001999 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002000 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2001 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002002 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002003 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2004 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002005 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002006 } else {
2007 DCHECK(second.IsConstant()) << second;
2008 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2009 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2010 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002011 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002012 break;
2013 }
2014
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002015 case Primitive::kPrimFloat: {
2016 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002017 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002018 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002019 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002020 }
2021
2022 case Primitive::kPrimDouble: {
2023 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002024 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002025 }
2026 break;
2027 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002028
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002029 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002030 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002031 }
2032}
2033
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002034void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002035 LocationSummary* locations =
2036 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002037 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002038 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002039 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002040 locations->SetInAt(0, Location::RequiresRegister());
2041 locations->SetInAt(1, Location::Any());
2042 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002043 break;
2044 }
Calin Juravle11351682014-10-23 15:38:15 +01002045 case Primitive::kPrimFloat:
2046 case Primitive::kPrimDouble: {
2047 locations->SetInAt(0, Location::RequiresFpuRegister());
2048 locations->SetInAt(1, Location::RequiresFpuRegister());
2049 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002050 break;
Calin Juravle11351682014-10-23 15:38:15 +01002051 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002052
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002053 default:
Calin Juravle11351682014-10-23 15:38:15 +01002054 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002055 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002056}
2057
2058void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2059 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002060 Location first = locations->InAt(0);
2061 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002062 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002063 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002064 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002065 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002066 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002067 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002068 __ subl(first.AsRegister<Register>(),
2069 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002070 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002071 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002072 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002073 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002074 }
2075
2076 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002077 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002078 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2079 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002080 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002081 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002082 __ sbbl(first.AsRegisterPairHigh<Register>(),
2083 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002084 } else {
2085 DCHECK(second.IsConstant()) << second;
2086 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2087 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2088 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002089 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002090 break;
2091 }
2092
Calin Juravle11351682014-10-23 15:38:15 +01002093 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002094 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002095 break;
Calin Juravle11351682014-10-23 15:38:15 +01002096 }
2097
2098 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002099 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002100 break;
2101 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002102
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002103 default:
Calin Juravle11351682014-10-23 15:38:15 +01002104 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002105 }
2106}
2107
Calin Juravle34bacdf2014-10-07 20:23:36 +01002108void LocationsBuilderX86::VisitMul(HMul* mul) {
2109 LocationSummary* locations =
2110 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2111 switch (mul->GetResultType()) {
2112 case Primitive::kPrimInt:
2113 locations->SetInAt(0, Location::RequiresRegister());
2114 locations->SetInAt(1, Location::Any());
2115 locations->SetOut(Location::SameAsFirstInput());
2116 break;
2117 case Primitive::kPrimLong: {
2118 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002119 locations->SetInAt(1, Location::Any());
2120 locations->SetOut(Location::SameAsFirstInput());
2121 // Needed for imul on 32bits with 64bits output.
2122 locations->AddTemp(Location::RegisterLocation(EAX));
2123 locations->AddTemp(Location::RegisterLocation(EDX));
2124 break;
2125 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002126 case Primitive::kPrimFloat:
2127 case Primitive::kPrimDouble: {
2128 locations->SetInAt(0, Location::RequiresFpuRegister());
2129 locations->SetInAt(1, Location::RequiresFpuRegister());
2130 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002131 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002132 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002133
2134 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002135 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002136 }
2137}
2138
2139void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2140 LocationSummary* locations = mul->GetLocations();
2141 Location first = locations->InAt(0);
2142 Location second = locations->InAt(1);
2143 DCHECK(first.Equals(locations->Out()));
2144
2145 switch (mul->GetResultType()) {
2146 case Primitive::kPrimInt: {
2147 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002148 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002149 } else if (second.IsConstant()) {
2150 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002151 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002152 } else {
2153 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002154 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002155 }
2156 break;
2157 }
2158
2159 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002160 Register in1_hi = first.AsRegisterPairHigh<Register>();
2161 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002162 Register eax = locations->GetTemp(0).AsRegister<Register>();
2163 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002164
2165 DCHECK_EQ(EAX, eax);
2166 DCHECK_EQ(EDX, edx);
2167
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002168 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002169 // output: in1
2170 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2171 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2172 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002173 if (second.IsConstant()) {
2174 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002175
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002176 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2177 int32_t low_value = Low32Bits(value);
2178 int32_t high_value = High32Bits(value);
2179 Immediate low(low_value);
2180 Immediate high(high_value);
2181
2182 __ movl(eax, high);
2183 // eax <- in1.lo * in2.hi
2184 __ imull(eax, in1_lo);
2185 // in1.hi <- in1.hi * in2.lo
2186 __ imull(in1_hi, low);
2187 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2188 __ addl(in1_hi, eax);
2189 // move in2_lo to eax to prepare for double precision
2190 __ movl(eax, low);
2191 // edx:eax <- in1.lo * in2.lo
2192 __ mull(in1_lo);
2193 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2194 __ addl(in1_hi, edx);
2195 // in1.lo <- (in1.lo * in2.lo)[31:0];
2196 __ movl(in1_lo, eax);
2197 } else if (second.IsRegisterPair()) {
2198 Register in2_hi = second.AsRegisterPairHigh<Register>();
2199 Register in2_lo = second.AsRegisterPairLow<Register>();
2200
2201 __ movl(eax, in2_hi);
2202 // eax <- in1.lo * in2.hi
2203 __ imull(eax, in1_lo);
2204 // in1.hi <- in1.hi * in2.lo
2205 __ imull(in1_hi, in2_lo);
2206 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2207 __ addl(in1_hi, eax);
2208 // move in1_lo to eax to prepare for double precision
2209 __ movl(eax, in1_lo);
2210 // edx:eax <- in1.lo * in2.lo
2211 __ mull(in2_lo);
2212 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2213 __ addl(in1_hi, edx);
2214 // in1.lo <- (in1.lo * in2.lo)[31:0];
2215 __ movl(in1_lo, eax);
2216 } else {
2217 DCHECK(second.IsDoubleStackSlot()) << second;
2218 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2219 Address in2_lo(ESP, second.GetStackIndex());
2220
2221 __ movl(eax, in2_hi);
2222 // eax <- in1.lo * in2.hi
2223 __ imull(eax, in1_lo);
2224 // in1.hi <- in1.hi * in2.lo
2225 __ imull(in1_hi, in2_lo);
2226 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2227 __ addl(in1_hi, eax);
2228 // move in1_lo to eax to prepare for double precision
2229 __ movl(eax, in1_lo);
2230 // edx:eax <- in1.lo * in2.lo
2231 __ mull(in2_lo);
2232 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2233 __ addl(in1_hi, edx);
2234 // in1.lo <- (in1.lo * in2.lo)[31:0];
2235 __ movl(in1_lo, eax);
2236 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002237
2238 break;
2239 }
2240
Calin Juravleb5bfa962014-10-21 18:02:24 +01002241 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002242 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002243 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002244 }
2245
2246 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002247 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002248 break;
2249 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002250
2251 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002252 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002253 }
2254}
2255
Roland Levillain232ade02015-04-20 15:14:36 +01002256void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2257 uint32_t temp_offset,
2258 uint32_t stack_adjustment,
2259 bool is_fp,
2260 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002261 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002262 DCHECK(!is_wide);
2263 if (is_fp) {
2264 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2265 } else {
2266 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2267 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002268 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002269 DCHECK(is_wide);
2270 if (is_fp) {
2271 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2272 } else {
2273 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2274 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002275 } else {
2276 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002277 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002278 Location stack_temp = Location::StackSlot(temp_offset);
2279 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002280 if (is_fp) {
2281 __ flds(Address(ESP, temp_offset));
2282 } else {
2283 __ filds(Address(ESP, temp_offset));
2284 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002285 } else {
2286 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2287 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002288 if (is_fp) {
2289 __ fldl(Address(ESP, temp_offset));
2290 } else {
2291 __ fildl(Address(ESP, temp_offset));
2292 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002293 }
2294 }
2295}
2296
2297void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2298 Primitive::Type type = rem->GetResultType();
2299 bool is_float = type == Primitive::kPrimFloat;
2300 size_t elem_size = Primitive::ComponentSize(type);
2301 LocationSummary* locations = rem->GetLocations();
2302 Location first = locations->InAt(0);
2303 Location second = locations->InAt(1);
2304 Location out = locations->Out();
2305
2306 // Create stack space for 2 elements.
2307 // TODO: enhance register allocator to ask for stack temporaries.
2308 __ subl(ESP, Immediate(2 * elem_size));
2309
2310 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002311 const bool is_wide = !is_float;
2312 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2313 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002314
2315 // Loop doing FPREM until we stabilize.
2316 Label retry;
2317 __ Bind(&retry);
2318 __ fprem();
2319
2320 // Move FP status to AX.
2321 __ fstsw();
2322
2323 // And see if the argument reduction is complete. This is signaled by the
2324 // C2 FPU flag bit set to 0.
2325 __ andl(EAX, Immediate(kC2ConditionMask));
2326 __ j(kNotEqual, &retry);
2327
2328 // We have settled on the final value. Retrieve it into an XMM register.
2329 // Store FP top of stack to real stack.
2330 if (is_float) {
2331 __ fsts(Address(ESP, 0));
2332 } else {
2333 __ fstl(Address(ESP, 0));
2334 }
2335
2336 // Pop the 2 items from the FP stack.
2337 __ fucompp();
2338
2339 // Load the value from the stack into an XMM register.
2340 DCHECK(out.IsFpuRegister()) << out;
2341 if (is_float) {
2342 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2343 } else {
2344 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2345 }
2346
2347 // And remove the temporary stack space we allocated.
2348 __ addl(ESP, Immediate(2 * elem_size));
2349}
2350
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002351
2352void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2353 DCHECK(instruction->IsDiv() || instruction->IsRem());
2354
2355 LocationSummary* locations = instruction->GetLocations();
2356 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002357 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002358
2359 Register out_register = locations->Out().AsRegister<Register>();
2360 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002361 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002362
2363 DCHECK(imm == 1 || imm == -1);
2364
2365 if (instruction->IsRem()) {
2366 __ xorl(out_register, out_register);
2367 } else {
2368 __ movl(out_register, input_register);
2369 if (imm == -1) {
2370 __ negl(out_register);
2371 }
2372 }
2373}
2374
2375
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002376void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002377 LocationSummary* locations = instruction->GetLocations();
2378
2379 Register out_register = locations->Out().AsRegister<Register>();
2380 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002381 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002382
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002383 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002384 Register num = locations->GetTemp(0).AsRegister<Register>();
2385
2386 __ leal(num, Address(input_register, std::abs(imm) - 1));
2387 __ testl(input_register, input_register);
2388 __ cmovl(kGreaterEqual, num, input_register);
2389 int shift = CTZ(imm);
2390 __ sarl(num, Immediate(shift));
2391
2392 if (imm < 0) {
2393 __ negl(num);
2394 }
2395
2396 __ movl(out_register, num);
2397}
2398
2399void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2400 DCHECK(instruction->IsDiv() || instruction->IsRem());
2401
2402 LocationSummary* locations = instruction->GetLocations();
2403 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2404
2405 Register eax = locations->InAt(0).AsRegister<Register>();
2406 Register out = locations->Out().AsRegister<Register>();
2407 Register num;
2408 Register edx;
2409
2410 if (instruction->IsDiv()) {
2411 edx = locations->GetTemp(0).AsRegister<Register>();
2412 num = locations->GetTemp(1).AsRegister<Register>();
2413 } else {
2414 edx = locations->Out().AsRegister<Register>();
2415 num = locations->GetTemp(0).AsRegister<Register>();
2416 }
2417
2418 DCHECK_EQ(EAX, eax);
2419 DCHECK_EQ(EDX, edx);
2420 if (instruction->IsDiv()) {
2421 DCHECK_EQ(EAX, out);
2422 } else {
2423 DCHECK_EQ(EDX, out);
2424 }
2425
2426 int64_t magic;
2427 int shift;
2428 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2429
2430 Label ndiv;
2431 Label end;
2432 // If numerator is 0, the result is 0, no computation needed.
2433 __ testl(eax, eax);
2434 __ j(kNotEqual, &ndiv);
2435
2436 __ xorl(out, out);
2437 __ jmp(&end);
2438
2439 __ Bind(&ndiv);
2440
2441 // Save the numerator.
2442 __ movl(num, eax);
2443
2444 // EAX = magic
2445 __ movl(eax, Immediate(magic));
2446
2447 // EDX:EAX = magic * numerator
2448 __ imull(num);
2449
2450 if (imm > 0 && magic < 0) {
2451 // EDX += num
2452 __ addl(edx, num);
2453 } else if (imm < 0 && magic > 0) {
2454 __ subl(edx, num);
2455 }
2456
2457 // Shift if needed.
2458 if (shift != 0) {
2459 __ sarl(edx, Immediate(shift));
2460 }
2461
2462 // EDX += 1 if EDX < 0
2463 __ movl(eax, edx);
2464 __ shrl(edx, Immediate(31));
2465 __ addl(edx, eax);
2466
2467 if (instruction->IsRem()) {
2468 __ movl(eax, num);
2469 __ imull(edx, Immediate(imm));
2470 __ subl(eax, edx);
2471 __ movl(edx, eax);
2472 } else {
2473 __ movl(eax, edx);
2474 }
2475 __ Bind(&end);
2476}
2477
Calin Juravlebacfec32014-11-14 15:54:36 +00002478void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2479 DCHECK(instruction->IsDiv() || instruction->IsRem());
2480
2481 LocationSummary* locations = instruction->GetLocations();
2482 Location out = locations->Out();
2483 Location first = locations->InAt(0);
2484 Location second = locations->InAt(1);
2485 bool is_div = instruction->IsDiv();
2486
2487 switch (instruction->GetResultType()) {
2488 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002489 DCHECK_EQ(EAX, first.AsRegister<Register>());
2490 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002491
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002492 if (instruction->InputAt(1)->IsIntConstant()) {
2493 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002494
2495 if (imm == 0) {
2496 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2497 } else if (imm == 1 || imm == -1) {
2498 DivRemOneOrMinusOne(instruction);
2499 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002500 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002501 } else {
2502 DCHECK(imm <= -2 || imm >= 2);
2503 GenerateDivRemWithAnyConstant(instruction);
2504 }
2505 } else {
2506 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002507 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002508 is_div);
2509 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002510
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002511 Register second_reg = second.AsRegister<Register>();
2512 // 0x80000000/-1 triggers an arithmetic exception!
2513 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2514 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002515
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002516 __ cmpl(second_reg, Immediate(-1));
2517 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002518
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002519 // edx:eax <- sign-extended of eax
2520 __ cdq();
2521 // eax = quotient, edx = remainder
2522 __ idivl(second_reg);
2523 __ Bind(slow_path->GetExitLabel());
2524 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002525 break;
2526 }
2527
2528 case Primitive::kPrimLong: {
2529 InvokeRuntimeCallingConvention calling_convention;
2530 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2531 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2532 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2533 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2534 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2535 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2536
2537 if (is_div) {
2538 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2539 } else {
2540 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2541 }
2542 uint32_t dex_pc = is_div
2543 ? instruction->AsDiv()->GetDexPc()
2544 : instruction->AsRem()->GetDexPc();
2545 codegen_->RecordPcInfo(instruction, dex_pc);
2546
2547 break;
2548 }
2549
2550 default:
2551 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2552 }
2553}
2554
Calin Juravle7c4954d2014-10-28 16:57:40 +00002555void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002556 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002557 ? LocationSummary::kCall
2558 : LocationSummary::kNoCall;
2559 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2560
Calin Juravle7c4954d2014-10-28 16:57:40 +00002561 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002562 case Primitive::kPrimInt: {
2563 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002564 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002565 locations->SetOut(Location::SameAsFirstInput());
2566 // Intel uses edx:eax as the dividend.
2567 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002568 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2569 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2570 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002571 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002572 locations->AddTemp(Location::RequiresRegister());
2573 }
Calin Juravled0d48522014-11-04 16:40:20 +00002574 break;
2575 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002576 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002577 InvokeRuntimeCallingConvention calling_convention;
2578 locations->SetInAt(0, Location::RegisterPairLocation(
2579 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2580 locations->SetInAt(1, Location::RegisterPairLocation(
2581 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2582 // Runtime helper puts the result in EAX, EDX.
2583 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002584 break;
2585 }
2586 case Primitive::kPrimFloat:
2587 case Primitive::kPrimDouble: {
2588 locations->SetInAt(0, Location::RequiresFpuRegister());
2589 locations->SetInAt(1, Location::RequiresFpuRegister());
2590 locations->SetOut(Location::SameAsFirstInput());
2591 break;
2592 }
2593
2594 default:
2595 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2596 }
2597}
2598
2599void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2600 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002601 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002602 Location first = locations->InAt(0);
2603 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002604
2605 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002606 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002607 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002608 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002609 break;
2610 }
2611
2612 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002613 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002614 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002615 break;
2616 }
2617
2618 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002619 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002620 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002621 break;
2622 }
2623
2624 default:
2625 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2626 }
2627}
2628
Calin Juravlebacfec32014-11-14 15:54:36 +00002629void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002630 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002631
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002632 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2633 ? LocationSummary::kCall
2634 : LocationSummary::kNoCall;
2635 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002636
Calin Juravled2ec87d2014-12-08 14:24:46 +00002637 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002638 case Primitive::kPrimInt: {
2639 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002640 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002641 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002642 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2643 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2644 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002645 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002646 locations->AddTemp(Location::RequiresRegister());
2647 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002648 break;
2649 }
2650 case Primitive::kPrimLong: {
2651 InvokeRuntimeCallingConvention calling_convention;
2652 locations->SetInAt(0, Location::RegisterPairLocation(
2653 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2654 locations->SetInAt(1, Location::RegisterPairLocation(
2655 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2656 // Runtime helper puts the result in EAX, EDX.
2657 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2658 break;
2659 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002660 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002661 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002662 locations->SetInAt(0, Location::Any());
2663 locations->SetInAt(1, Location::Any());
2664 locations->SetOut(Location::RequiresFpuRegister());
2665 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002666 break;
2667 }
2668
2669 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002670 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002671 }
2672}
2673
2674void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2675 Primitive::Type type = rem->GetResultType();
2676 switch (type) {
2677 case Primitive::kPrimInt:
2678 case Primitive::kPrimLong: {
2679 GenerateDivRemIntegral(rem);
2680 break;
2681 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002682 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002683 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002684 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002685 break;
2686 }
2687 default:
2688 LOG(FATAL) << "Unexpected rem type " << type;
2689 }
2690}
2691
Calin Juravled0d48522014-11-04 16:40:20 +00002692void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2693 LocationSummary* locations =
2694 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002695 switch (instruction->GetType()) {
2696 case Primitive::kPrimInt: {
2697 locations->SetInAt(0, Location::Any());
2698 break;
2699 }
2700 case Primitive::kPrimLong: {
2701 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2702 if (!instruction->IsConstant()) {
2703 locations->AddTemp(Location::RequiresRegister());
2704 }
2705 break;
2706 }
2707 default:
2708 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2709 }
Calin Juravled0d48522014-11-04 16:40:20 +00002710 if (instruction->HasUses()) {
2711 locations->SetOut(Location::SameAsFirstInput());
2712 }
2713}
2714
2715void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2716 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2717 codegen_->AddSlowPath(slow_path);
2718
2719 LocationSummary* locations = instruction->GetLocations();
2720 Location value = locations->InAt(0);
2721
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002722 switch (instruction->GetType()) {
2723 case Primitive::kPrimInt: {
2724 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002725 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002726 __ j(kEqual, slow_path->GetEntryLabel());
2727 } else if (value.IsStackSlot()) {
2728 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2729 __ j(kEqual, slow_path->GetEntryLabel());
2730 } else {
2731 DCHECK(value.IsConstant()) << value;
2732 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2733 __ jmp(slow_path->GetEntryLabel());
2734 }
2735 }
2736 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002737 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002738 case Primitive::kPrimLong: {
2739 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002740 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002741 __ movl(temp, value.AsRegisterPairLow<Register>());
2742 __ orl(temp, value.AsRegisterPairHigh<Register>());
2743 __ j(kEqual, slow_path->GetEntryLabel());
2744 } else {
2745 DCHECK(value.IsConstant()) << value;
2746 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2747 __ jmp(slow_path->GetEntryLabel());
2748 }
2749 }
2750 break;
2751 }
2752 default:
2753 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002754 }
Calin Juravled0d48522014-11-04 16:40:20 +00002755}
2756
Calin Juravle9aec02f2014-11-18 23:06:35 +00002757void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2758 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2759
2760 LocationSummary* locations =
2761 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2762
2763 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00002764 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00002765 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002766 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00002767 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00002768 // The shift count needs to be in CL or a constant.
2769 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002770 locations->SetOut(Location::SameAsFirstInput());
2771 break;
2772 }
2773 default:
2774 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2775 }
2776}
2777
2778void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2779 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2780
2781 LocationSummary* locations = op->GetLocations();
2782 Location first = locations->InAt(0);
2783 Location second = locations->InAt(1);
2784 DCHECK(first.Equals(locations->Out()));
2785
2786 switch (op->GetResultType()) {
2787 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00002788 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002789 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002790 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002791 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002792 DCHECK_EQ(ECX, second_reg);
2793 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002794 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002795 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002796 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002797 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002798 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002799 }
2800 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002801 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
2802 if (shift == 0) {
2803 return;
2804 }
2805 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002806 if (op->IsShl()) {
2807 __ shll(first_reg, imm);
2808 } else if (op->IsShr()) {
2809 __ sarl(first_reg, imm);
2810 } else {
2811 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002812 }
2813 }
2814 break;
2815 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002816 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002817 if (second.IsRegister()) {
2818 Register second_reg = second.AsRegister<Register>();
2819 DCHECK_EQ(ECX, second_reg);
2820 if (op->IsShl()) {
2821 GenerateShlLong(first, second_reg);
2822 } else if (op->IsShr()) {
2823 GenerateShrLong(first, second_reg);
2824 } else {
2825 GenerateUShrLong(first, second_reg);
2826 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002827 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002828 // Shift by a constant.
2829 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
2830 // Nothing to do if the shift is 0, as the input is already the output.
2831 if (shift != 0) {
2832 if (op->IsShl()) {
2833 GenerateShlLong(first, shift);
2834 } else if (op->IsShr()) {
2835 GenerateShrLong(first, shift);
2836 } else {
2837 GenerateUShrLong(first, shift);
2838 }
2839 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002840 }
2841 break;
2842 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002843 default:
2844 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2845 }
2846}
2847
Mark P Mendell73945692015-04-29 14:56:17 +00002848void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
2849 Register low = loc.AsRegisterPairLow<Register>();
2850 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04002851 if (shift == 1) {
2852 // This is just an addition.
2853 __ addl(low, low);
2854 __ adcl(high, high);
2855 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00002856 // Shift by 32 is easy. High gets low, and low gets 0.
2857 codegen_->EmitParallelMoves(
2858 loc.ToLow(),
2859 loc.ToHigh(),
2860 Primitive::kPrimInt,
2861 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2862 loc.ToLow(),
2863 Primitive::kPrimInt);
2864 } else if (shift > 32) {
2865 // Low part becomes 0. High part is low part << (shift-32).
2866 __ movl(high, low);
2867 __ shll(high, Immediate(shift - 32));
2868 __ xorl(low, low);
2869 } else {
2870 // Between 1 and 31.
2871 __ shld(high, low, Immediate(shift));
2872 __ shll(low, Immediate(shift));
2873 }
2874}
2875
Calin Juravle9aec02f2014-11-18 23:06:35 +00002876void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2877 Label done;
2878 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2879 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2880 __ testl(shifter, Immediate(32));
2881 __ j(kEqual, &done);
2882 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2883 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2884 __ Bind(&done);
2885}
2886
Mark P Mendell73945692015-04-29 14:56:17 +00002887void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
2888 Register low = loc.AsRegisterPairLow<Register>();
2889 Register high = loc.AsRegisterPairHigh<Register>();
2890 if (shift == 32) {
2891 // Need to copy the sign.
2892 DCHECK_NE(low, high);
2893 __ movl(low, high);
2894 __ sarl(high, Immediate(31));
2895 } else if (shift > 32) {
2896 DCHECK_NE(low, high);
2897 // High part becomes sign. Low part is shifted by shift - 32.
2898 __ movl(low, high);
2899 __ sarl(high, Immediate(31));
2900 __ sarl(low, Immediate(shift - 32));
2901 } else {
2902 // Between 1 and 31.
2903 __ shrd(low, high, Immediate(shift));
2904 __ sarl(high, Immediate(shift));
2905 }
2906}
2907
Calin Juravle9aec02f2014-11-18 23:06:35 +00002908void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2909 Label done;
2910 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2911 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2912 __ testl(shifter, Immediate(32));
2913 __ j(kEqual, &done);
2914 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2915 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2916 __ Bind(&done);
2917}
2918
Mark P Mendell73945692015-04-29 14:56:17 +00002919void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
2920 Register low = loc.AsRegisterPairLow<Register>();
2921 Register high = loc.AsRegisterPairHigh<Register>();
2922 if (shift == 32) {
2923 // Shift by 32 is easy. Low gets high, and high gets 0.
2924 codegen_->EmitParallelMoves(
2925 loc.ToHigh(),
2926 loc.ToLow(),
2927 Primitive::kPrimInt,
2928 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2929 loc.ToHigh(),
2930 Primitive::kPrimInt);
2931 } else if (shift > 32) {
2932 // Low part is high >> (shift - 32). High part becomes 0.
2933 __ movl(low, high);
2934 __ shrl(low, Immediate(shift - 32));
2935 __ xorl(high, high);
2936 } else {
2937 // Between 1 and 31.
2938 __ shrd(low, high, Immediate(shift));
2939 __ shrl(high, Immediate(shift));
2940 }
2941}
2942
Calin Juravle9aec02f2014-11-18 23:06:35 +00002943void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2944 Label done;
2945 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2946 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2947 __ testl(shifter, Immediate(32));
2948 __ j(kEqual, &done);
2949 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2950 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2951 __ Bind(&done);
2952}
2953
2954void LocationsBuilderX86::VisitShl(HShl* shl) {
2955 HandleShift(shl);
2956}
2957
2958void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2959 HandleShift(shl);
2960}
2961
2962void LocationsBuilderX86::VisitShr(HShr* shr) {
2963 HandleShift(shr);
2964}
2965
2966void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2967 HandleShift(shr);
2968}
2969
2970void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2971 HandleShift(ushr);
2972}
2973
2974void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2975 HandleShift(ushr);
2976}
2977
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002978void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002979 LocationSummary* locations =
2980 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002981 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002982 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002983 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002984 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002985}
2986
2987void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2988 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002989 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002990 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002991
Nicolas Geoffray39468442014-09-02 15:17:15 +01002992 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002993 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002994}
2995
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002996void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2997 LocationSummary* locations =
2998 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2999 locations->SetOut(Location::RegisterLocation(EAX));
3000 InvokeRuntimeCallingConvention calling_convention;
3001 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003002 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003003 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003004}
3005
3006void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3007 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003008 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3009
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003010 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003011
3012 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3013 DCHECK(!codegen_->IsLeafMethod());
3014}
3015
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003016void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003017 LocationSummary* locations =
3018 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003019 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3020 if (location.IsStackSlot()) {
3021 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3022 } else if (location.IsDoubleStackSlot()) {
3023 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003024 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003025 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003026}
3027
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003028void InstructionCodeGeneratorX86::VisitParameterValue(
3029 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3030}
3031
3032void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3033 LocationSummary* locations =
3034 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3035 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3036}
3037
3038void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003039}
3040
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003041void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003042 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003043 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003044 locations->SetInAt(0, Location::RequiresRegister());
3045 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003046}
3047
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003048void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3049 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003050 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003051 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003052 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003053 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003054 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003055 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003056 break;
3057
3058 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003059 __ notl(out.AsRegisterPairLow<Register>());
3060 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003061 break;
3062
3063 default:
3064 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3065 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003066}
3067
David Brazdil66d126e2015-04-03 16:02:44 +01003068void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3069 LocationSummary* locations =
3070 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3071 locations->SetInAt(0, Location::RequiresRegister());
3072 locations->SetOut(Location::SameAsFirstInput());
3073}
3074
3075void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003076 LocationSummary* locations = bool_not->GetLocations();
3077 Location in = locations->InAt(0);
3078 Location out = locations->Out();
3079 DCHECK(in.Equals(out));
3080 __ xorl(out.AsRegister<Register>(), Immediate(1));
3081}
3082
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003083void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003084 LocationSummary* locations =
3085 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003086 switch (compare->InputAt(0)->GetType()) {
3087 case Primitive::kPrimLong: {
3088 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003089 locations->SetInAt(1, Location::Any());
3090 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3091 break;
3092 }
3093 case Primitive::kPrimFloat:
3094 case Primitive::kPrimDouble: {
3095 locations->SetInAt(0, Location::RequiresFpuRegister());
3096 locations->SetInAt(1, Location::RequiresFpuRegister());
3097 locations->SetOut(Location::RequiresRegister());
3098 break;
3099 }
3100 default:
3101 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3102 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003103}
3104
3105void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003106 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003107 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003108 Location left = locations->InAt(0);
3109 Location right = locations->InAt(1);
3110
3111 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003112 switch (compare->InputAt(0)->GetType()) {
3113 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003114 Register left_low = left.AsRegisterPairLow<Register>();
3115 Register left_high = left.AsRegisterPairHigh<Register>();
3116 int32_t val_low = 0;
3117 int32_t val_high = 0;
3118 bool right_is_const = false;
3119
3120 if (right.IsConstant()) {
3121 DCHECK(right.GetConstant()->IsLongConstant());
3122 right_is_const = true;
3123 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3124 val_low = Low32Bits(val);
3125 val_high = High32Bits(val);
3126 }
3127
Calin Juravleddb7df22014-11-25 20:56:51 +00003128 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003129 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003130 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003131 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003132 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003133 DCHECK(right_is_const) << right;
3134 if (val_high == 0) {
3135 __ testl(left_high, left_high);
3136 } else {
3137 __ cmpl(left_high, Immediate(val_high));
3138 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003139 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003140 __ j(kLess, &less); // Signed compare.
3141 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003142 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003143 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003144 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003145 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003146 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003147 DCHECK(right_is_const) << right;
3148 if (val_low == 0) {
3149 __ testl(left_low, left_low);
3150 } else {
3151 __ cmpl(left_low, Immediate(val_low));
3152 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003153 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003154 break;
3155 }
3156 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003157 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003158 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3159 break;
3160 }
3161 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003162 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003163 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003164 break;
3165 }
3166 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003167 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003168 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003169 __ movl(out, Immediate(0));
3170 __ j(kEqual, &done);
3171 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3172
3173 __ Bind(&greater);
3174 __ movl(out, Immediate(1));
3175 __ jmp(&done);
3176
3177 __ Bind(&less);
3178 __ movl(out, Immediate(-1));
3179
3180 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003181}
3182
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003183void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003184 LocationSummary* locations =
3185 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003186 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3187 locations->SetInAt(i, Location::Any());
3188 }
3189 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003190}
3191
3192void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003193 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003194 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003195}
3196
Calin Juravle52c48962014-12-16 17:02:57 +00003197void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3198 /*
3199 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3200 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3201 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3202 */
3203 switch (kind) {
3204 case MemBarrierKind::kAnyAny: {
3205 __ mfence();
3206 break;
3207 }
3208 case MemBarrierKind::kAnyStore:
3209 case MemBarrierKind::kLoadAny:
3210 case MemBarrierKind::kStoreStore: {
3211 // nop
3212 break;
3213 }
3214 default:
3215 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003216 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003217}
3218
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003219
Mark Mendell09ed1a32015-03-25 08:30:06 -04003220void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003221 Location temp) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04003222 // TODO: Implement all kinds of calls:
3223 // 1) boot -> boot
3224 // 2) app -> boot
3225 // 3) app -> app
3226 //
3227 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Jeff Hao848f70a2014-01-15 13:49:50 -08003228
3229 if (invoke->IsStringInit()) {
3230 // temp = thread->string_init_entrypoint
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003231 Register reg = temp.AsRegister<Register>();
3232 __ fs()->movl(reg, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003233 // (temp + offset_of_quick_compiled_code)()
3234 __ call(Address(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003235 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3236 } else if (invoke->IsRecursive()) {
3237 __ call(GetFrameEntryLabel());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003238 } else {
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003239 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3240
3241 Register method_reg;
3242 Register reg = temp.AsRegister<Register>();
3243 if (current_method.IsRegister()) {
3244 method_reg = current_method.AsRegister<Register>();
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003245 } else {
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01003246 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003247 DCHECK(!current_method.IsValid());
3248 method_reg = reg;
3249 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003250 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003251 // temp = temp->dex_cache_resolved_methods_;
3252 __ movl(reg, Address(method_reg, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3253 // temp = temp[index_in_cache]
3254 __ movl(reg, Address(reg,
3255 CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
3256 // (temp + offset_of_quick_compiled_code)()
3257 __ call(Address(reg,
3258 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003259 }
3260
3261 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003262}
3263
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003264void CodeGeneratorX86::MarkGCCard(Register temp,
3265 Register card,
3266 Register object,
3267 Register value,
3268 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003269 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003270 if (value_can_be_null) {
3271 __ testl(value, value);
3272 __ j(kEqual, &is_null);
3273 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003274 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3275 __ movl(temp, object);
3276 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003277 __ movb(Address(temp, card, TIMES_1, 0),
3278 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003279 if (value_can_be_null) {
3280 __ Bind(&is_null);
3281 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003282}
3283
Calin Juravle52c48962014-12-16 17:02:57 +00003284void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3285 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003286 LocationSummary* locations =
3287 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003288 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003289
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003290 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3291 locations->SetOut(Location::RequiresFpuRegister());
3292 } else {
3293 // The output overlaps in case of long: we don't want the low move to overwrite
3294 // the object's location.
3295 locations->SetOut(Location::RequiresRegister(),
3296 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3297 : Location::kNoOutputOverlap);
3298 }
Calin Juravle52c48962014-12-16 17:02:57 +00003299
3300 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3301 // Long values can be loaded atomically into an XMM using movsd.
3302 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3303 // and then copy the XMM into the output 32bits at a time).
3304 locations->AddTemp(Location::RequiresFpuRegister());
3305 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003306}
3307
Calin Juravle52c48962014-12-16 17:02:57 +00003308void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3309 const FieldInfo& field_info) {
3310 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003311
Calin Juravle52c48962014-12-16 17:02:57 +00003312 LocationSummary* locations = instruction->GetLocations();
3313 Register base = locations->InAt(0).AsRegister<Register>();
3314 Location out = locations->Out();
3315 bool is_volatile = field_info.IsVolatile();
3316 Primitive::Type field_type = field_info.GetFieldType();
3317 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3318
3319 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003320 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003321 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003322 break;
3323 }
3324
3325 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003326 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003327 break;
3328 }
3329
3330 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003331 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003332 break;
3333 }
3334
3335 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003336 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003337 break;
3338 }
3339
3340 case Primitive::kPrimInt:
3341 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003342 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003343 break;
3344 }
3345
3346 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003347 if (is_volatile) {
3348 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3349 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003350 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003351 __ movd(out.AsRegisterPairLow<Register>(), temp);
3352 __ psrlq(temp, Immediate(32));
3353 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3354 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003355 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003356 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003357 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003358 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3359 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003360 break;
3361 }
3362
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003363 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003364 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003365 break;
3366 }
3367
3368 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003369 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003370 break;
3371 }
3372
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003373 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003374 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003375 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003376 }
Calin Juravle52c48962014-12-16 17:02:57 +00003377
Calin Juravle77520bc2015-01-12 18:45:46 +00003378 // Longs are handled in the switch.
3379 if (field_type != Primitive::kPrimLong) {
3380 codegen_->MaybeRecordImplicitNullCheck(instruction);
3381 }
3382
Calin Juravle52c48962014-12-16 17:02:57 +00003383 if (is_volatile) {
3384 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3385 }
3386}
3387
3388void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3389 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3390
3391 LocationSummary* locations =
3392 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3393 locations->SetInAt(0, Location::RequiresRegister());
3394 bool is_volatile = field_info.IsVolatile();
3395 Primitive::Type field_type = field_info.GetFieldType();
3396 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3397 || (field_type == Primitive::kPrimByte);
3398
3399 // The register allocator does not support multiple
3400 // inputs that die at entry with one in a specific register.
3401 if (is_byte_type) {
3402 // Ensure the value is in a byte register.
3403 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003404 } else if (Primitive::IsFloatingPointType(field_type)) {
3405 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003406 } else {
3407 locations->SetInAt(1, Location::RequiresRegister());
3408 }
3409 // Temporary registers for the write barrier.
3410 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3411 locations->AddTemp(Location::RequiresRegister());
3412 // Ensure the card is in a byte register.
3413 locations->AddTemp(Location::RegisterLocation(ECX));
3414 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3415 // 64bits value can be atomically written to an address with movsd and an XMM register.
3416 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3417 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3418 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3419 // isolated cases when we need this it isn't worth adding the extra complexity.
3420 locations->AddTemp(Location::RequiresFpuRegister());
3421 locations->AddTemp(Location::RequiresFpuRegister());
3422 }
3423}
3424
3425void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003426 const FieldInfo& field_info,
3427 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003428 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3429
3430 LocationSummary* locations = instruction->GetLocations();
3431 Register base = locations->InAt(0).AsRegister<Register>();
3432 Location value = locations->InAt(1);
3433 bool is_volatile = field_info.IsVolatile();
3434 Primitive::Type field_type = field_info.GetFieldType();
3435 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3436
3437 if (is_volatile) {
3438 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3439 }
3440
3441 switch (field_type) {
3442 case Primitive::kPrimBoolean:
3443 case Primitive::kPrimByte: {
3444 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3445 break;
3446 }
3447
3448 case Primitive::kPrimShort:
3449 case Primitive::kPrimChar: {
3450 __ movw(Address(base, offset), value.AsRegister<Register>());
3451 break;
3452 }
3453
3454 case Primitive::kPrimInt:
3455 case Primitive::kPrimNot: {
3456 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003457 break;
3458 }
3459
3460 case Primitive::kPrimLong: {
3461 if (is_volatile) {
3462 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3463 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3464 __ movd(temp1, value.AsRegisterPairLow<Register>());
3465 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3466 __ punpckldq(temp1, temp2);
3467 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003468 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003469 } else {
3470 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003471 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003472 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3473 }
3474 break;
3475 }
3476
3477 case Primitive::kPrimFloat: {
3478 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3479 break;
3480 }
3481
3482 case Primitive::kPrimDouble: {
3483 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3484 break;
3485 }
3486
3487 case Primitive::kPrimVoid:
3488 LOG(FATAL) << "Unreachable type " << field_type;
3489 UNREACHABLE();
3490 }
3491
Calin Juravle77520bc2015-01-12 18:45:46 +00003492 // Longs are handled in the switch.
3493 if (field_type != Primitive::kPrimLong) {
3494 codegen_->MaybeRecordImplicitNullCheck(instruction);
3495 }
3496
3497 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3498 Register temp = locations->GetTemp(0).AsRegister<Register>();
3499 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003500 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003501 }
3502
Calin Juravle52c48962014-12-16 17:02:57 +00003503 if (is_volatile) {
3504 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3505 }
3506}
3507
3508void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3509 HandleFieldGet(instruction, instruction->GetFieldInfo());
3510}
3511
3512void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3513 HandleFieldGet(instruction, instruction->GetFieldInfo());
3514}
3515
3516void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3517 HandleFieldSet(instruction, instruction->GetFieldInfo());
3518}
3519
3520void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003521 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003522}
3523
3524void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3525 HandleFieldSet(instruction, instruction->GetFieldInfo());
3526}
3527
3528void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003529 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003530}
3531
3532void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3533 HandleFieldGet(instruction, instruction->GetFieldInfo());
3534}
3535
3536void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3537 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003538}
3539
3540void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003541 LocationSummary* locations =
3542 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003543 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3544 ? Location::RequiresRegister()
3545 : Location::Any();
3546 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003547 if (instruction->HasUses()) {
3548 locations->SetOut(Location::SameAsFirstInput());
3549 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003550}
3551
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003552void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003553 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3554 return;
3555 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003556 LocationSummary* locations = instruction->GetLocations();
3557 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003558
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003559 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3560 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3561}
3562
3563void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003564 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003565 codegen_->AddSlowPath(slow_path);
3566
3567 LocationSummary* locations = instruction->GetLocations();
3568 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003569
3570 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003571 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003572 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003573 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003574 } else {
3575 DCHECK(obj.IsConstant()) << obj;
3576 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3577 __ jmp(slow_path->GetEntryLabel());
3578 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003579 }
3580 __ j(kEqual, slow_path->GetEntryLabel());
3581}
3582
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003583void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3584 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3585 GenerateImplicitNullCheck(instruction);
3586 } else {
3587 GenerateExplicitNullCheck(instruction);
3588 }
3589}
3590
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003591void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003592 LocationSummary* locations =
3593 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003594 locations->SetInAt(0, Location::RequiresRegister());
3595 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003596 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3597 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3598 } else {
3599 // The output overlaps in case of long: we don't want the low move to overwrite
3600 // the array's location.
3601 locations->SetOut(Location::RequiresRegister(),
3602 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3603 : Location::kNoOutputOverlap);
3604 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003605}
3606
3607void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3608 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003609 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003610 Location index = locations->InAt(1);
3611
Calin Juravle77520bc2015-01-12 18:45:46 +00003612 Primitive::Type type = instruction->GetType();
3613 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003614 case Primitive::kPrimBoolean: {
3615 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003616 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003617 if (index.IsConstant()) {
3618 __ movzxb(out, Address(obj,
3619 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3620 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003621 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003622 }
3623 break;
3624 }
3625
3626 case Primitive::kPrimByte: {
3627 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003628 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003629 if (index.IsConstant()) {
3630 __ movsxb(out, Address(obj,
3631 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3632 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003633 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003634 }
3635 break;
3636 }
3637
3638 case Primitive::kPrimShort: {
3639 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003640 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003641 if (index.IsConstant()) {
3642 __ movsxw(out, Address(obj,
3643 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3644 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003645 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003646 }
3647 break;
3648 }
3649
3650 case Primitive::kPrimChar: {
3651 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003652 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003653 if (index.IsConstant()) {
3654 __ movzxw(out, Address(obj,
3655 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3656 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003657 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003658 }
3659 break;
3660 }
3661
3662 case Primitive::kPrimInt:
3663 case Primitive::kPrimNot: {
3664 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003665 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003666 if (index.IsConstant()) {
3667 __ movl(out, Address(obj,
3668 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3669 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003670 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003671 }
3672 break;
3673 }
3674
3675 case Primitive::kPrimLong: {
3676 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003677 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003678 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003679 if (index.IsConstant()) {
3680 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003681 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003682 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003683 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003684 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003685 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003686 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003687 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003688 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003689 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003690 }
3691 break;
3692 }
3693
Mark Mendell7c8d0092015-01-26 11:21:33 -05003694 case Primitive::kPrimFloat: {
3695 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3696 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3697 if (index.IsConstant()) {
3698 __ movss(out, Address(obj,
3699 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3700 } else {
3701 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3702 }
3703 break;
3704 }
3705
3706 case Primitive::kPrimDouble: {
3707 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3708 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3709 if (index.IsConstant()) {
3710 __ movsd(out, Address(obj,
3711 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3712 } else {
3713 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3714 }
3715 break;
3716 }
3717
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003718 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003719 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003720 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003721 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003722
3723 if (type != Primitive::kPrimLong) {
3724 codegen_->MaybeRecordImplicitNullCheck(instruction);
3725 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003726}
3727
3728void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003729 // This location builder might end up asking to up to four registers, which is
3730 // not currently possible for baseline. The situation in which we need four
3731 // registers cannot be met by baseline though, because it has not run any
3732 // optimization.
3733
Nicolas Geoffray39468442014-09-02 15:17:15 +01003734 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003735 bool needs_write_barrier =
3736 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3737
Mark Mendell5f874182015-03-04 15:42:45 -05003738 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003739
Nicolas Geoffray39468442014-09-02 15:17:15 +01003740 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3741 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003742 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003743
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003744 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003745 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003746 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3747 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3748 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003749 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003750 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3751 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003752 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003753 // In case of a byte operation, the register allocator does not support multiple
3754 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003755 locations->SetInAt(0, Location::RequiresRegister());
3756 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003757 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003758 // Ensure the value is in a byte register.
3759 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003760 } else if (Primitive::IsFloatingPointType(value_type)) {
3761 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003762 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003763 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003764 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003765 // Temporary registers for the write barrier.
3766 if (needs_write_barrier) {
3767 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003768 // Ensure the card is in a byte register.
3769 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003770 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003771 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003772}
3773
3774void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3775 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003776 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003777 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003778 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003779 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003780 bool needs_runtime_call = locations->WillCall();
3781 bool needs_write_barrier =
3782 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003783
3784 switch (value_type) {
3785 case Primitive::kPrimBoolean:
3786 case Primitive::kPrimByte: {
3787 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003788 if (index.IsConstant()) {
3789 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003790 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003791 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003792 } else {
3793 __ movb(Address(obj, offset),
3794 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3795 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003796 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003797 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003798 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003799 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003800 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003801 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003802 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3803 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003804 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003805 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003806 break;
3807 }
3808
3809 case Primitive::kPrimShort:
3810 case Primitive::kPrimChar: {
3811 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003812 if (index.IsConstant()) {
3813 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003814 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003815 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003816 } else {
3817 __ movw(Address(obj, offset),
3818 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3819 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003820 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003821 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003822 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3823 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003824 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003825 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003826 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3827 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003828 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003829 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003830 break;
3831 }
3832
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003833 case Primitive::kPrimInt:
3834 case Primitive::kPrimNot: {
3835 if (!needs_runtime_call) {
3836 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3837 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003838 size_t offset =
3839 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003840 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003841 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003842 } else {
3843 DCHECK(value.IsConstant()) << value;
3844 __ movl(Address(obj, offset),
3845 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3846 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003847 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003848 DCHECK(index.IsRegister()) << index;
3849 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003850 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3851 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003852 } else {
3853 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003854 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003855 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3856 }
3857 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003858 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003859
3860 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003861 Register temp = locations->GetTemp(0).AsRegister<Register>();
3862 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003863 codegen_->MarkGCCard(
3864 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003865 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003866 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003867 DCHECK_EQ(value_type, Primitive::kPrimNot);
3868 DCHECK(!codegen_->IsLeafMethod());
3869 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3870 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003871 }
3872 break;
3873 }
3874
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003875 case Primitive::kPrimLong: {
3876 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003877 if (index.IsConstant()) {
3878 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003879 if (value.IsRegisterPair()) {
3880 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003881 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003882 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003883 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003884 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003885 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3886 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003887 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003888 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3889 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003890 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003891 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003892 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003893 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003894 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003895 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003896 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003897 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003898 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003899 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003900 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003901 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003902 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003903 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003904 Immediate(High32Bits(val)));
3905 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003906 }
3907 break;
3908 }
3909
Mark Mendell7c8d0092015-01-26 11:21:33 -05003910 case Primitive::kPrimFloat: {
3911 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3912 DCHECK(value.IsFpuRegister());
3913 if (index.IsConstant()) {
3914 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3915 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3916 } else {
3917 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3918 value.AsFpuRegister<XmmRegister>());
3919 }
3920 break;
3921 }
3922
3923 case Primitive::kPrimDouble: {
3924 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3925 DCHECK(value.IsFpuRegister());
3926 if (index.IsConstant()) {
3927 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3928 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3929 } else {
3930 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3931 value.AsFpuRegister<XmmRegister>());
3932 }
3933 break;
3934 }
3935
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003936 case Primitive::kPrimVoid:
3937 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003938 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003939 }
3940}
3941
3942void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3943 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003944 locations->SetInAt(0, Location::RequiresRegister());
3945 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003946}
3947
3948void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3949 LocationSummary* locations = instruction->GetLocations();
3950 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003951 Register obj = locations->InAt(0).AsRegister<Register>();
3952 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003953 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003954 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003955}
3956
3957void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003958 LocationSummary* locations =
3959 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003960 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003961 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003962 if (instruction->HasUses()) {
3963 locations->SetOut(Location::SameAsFirstInput());
3964 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003965}
3966
3967void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3968 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003969 Location index_loc = locations->InAt(0);
3970 Location length_loc = locations->InAt(1);
3971 SlowPathCodeX86* slow_path =
3972 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003973
Mark Mendell99dbd682015-04-22 16:18:52 -04003974 if (length_loc.IsConstant()) {
3975 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3976 if (index_loc.IsConstant()) {
3977 // BCE will remove the bounds check if we are guarenteed to pass.
3978 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3979 if (index < 0 || index >= length) {
3980 codegen_->AddSlowPath(slow_path);
3981 __ jmp(slow_path->GetEntryLabel());
3982 } else {
3983 // Some optimization after BCE may have generated this, and we should not
3984 // generate a bounds check if it is a valid range.
3985 }
3986 return;
3987 }
3988
3989 // We have to reverse the jump condition because the length is the constant.
3990 Register index_reg = index_loc.AsRegister<Register>();
3991 __ cmpl(index_reg, Immediate(length));
3992 codegen_->AddSlowPath(slow_path);
3993 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003994 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003995 Register length = length_loc.AsRegister<Register>();
3996 if (index_loc.IsConstant()) {
3997 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3998 __ cmpl(length, Immediate(value));
3999 } else {
4000 __ cmpl(length, index_loc.AsRegister<Register>());
4001 }
4002 codegen_->AddSlowPath(slow_path);
4003 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004004 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004005}
4006
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004007void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4008 temp->SetLocations(nullptr);
4009}
4010
4011void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4012 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004013 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004014}
4015
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004016void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004017 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004018 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004019}
4020
4021void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004022 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4023}
4024
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004025void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4026 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4027}
4028
4029void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004030 HBasicBlock* block = instruction->GetBlock();
4031 if (block->GetLoopInformation() != nullptr) {
4032 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4033 // The back edge will generate the suspend check.
4034 return;
4035 }
4036 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4037 // The goto will generate the suspend check.
4038 return;
4039 }
4040 GenerateSuspendCheck(instruction, nullptr);
4041}
4042
4043void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4044 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004045 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004046 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4047 if (slow_path == nullptr) {
4048 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4049 instruction->SetSlowPath(slow_path);
4050 codegen_->AddSlowPath(slow_path);
4051 if (successor != nullptr) {
4052 DCHECK(successor->IsLoopHeader());
4053 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4054 }
4055 } else {
4056 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4057 }
4058
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004059 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004060 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004061 if (successor == nullptr) {
4062 __ j(kNotEqual, slow_path->GetEntryLabel());
4063 __ Bind(slow_path->GetReturnLabel());
4064 } else {
4065 __ j(kEqual, codegen_->GetLabelOf(successor));
4066 __ jmp(slow_path->GetEntryLabel());
4067 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004068}
4069
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004070X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4071 return codegen_->GetAssembler();
4072}
4073
Mark Mendell7c8d0092015-01-26 11:21:33 -05004074void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004075 ScratchRegisterScope ensure_scratch(
4076 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4077 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4078 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4079 __ movl(temp_reg, Address(ESP, src + stack_offset));
4080 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004081}
4082
4083void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004084 ScratchRegisterScope ensure_scratch(
4085 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4086 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4087 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4088 __ movl(temp_reg, Address(ESP, src + stack_offset));
4089 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4090 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4091 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004092}
4093
4094void ParallelMoveResolverX86::EmitMove(size_t index) {
4095 MoveOperands* move = moves_.Get(index);
4096 Location source = move->GetSource();
4097 Location destination = move->GetDestination();
4098
4099 if (source.IsRegister()) {
4100 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004101 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004102 } else {
4103 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004104 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004105 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004106 } else if (source.IsFpuRegister()) {
4107 if (destination.IsFpuRegister()) {
4108 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4109 } else if (destination.IsStackSlot()) {
4110 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4111 } else {
4112 DCHECK(destination.IsDoubleStackSlot());
4113 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4114 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004115 } else if (source.IsStackSlot()) {
4116 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004117 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004118 } else if (destination.IsFpuRegister()) {
4119 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004120 } else {
4121 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004122 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4123 }
4124 } else if (source.IsDoubleStackSlot()) {
4125 if (destination.IsFpuRegister()) {
4126 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4127 } else {
4128 DCHECK(destination.IsDoubleStackSlot()) << destination;
4129 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004130 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004131 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004132 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004133 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004134 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004135 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004136 if (value == 0) {
4137 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4138 } else {
4139 __ movl(destination.AsRegister<Register>(), Immediate(value));
4140 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004141 } else {
4142 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004143 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004144 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004145 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004146 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004147 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004148 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004149 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004150 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4151 if (value == 0) {
4152 // Easy handling of 0.0.
4153 __ xorps(dest, dest);
4154 } else {
4155 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004156 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4157 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4158 __ movl(temp, Immediate(value));
4159 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004160 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004161 } else {
4162 DCHECK(destination.IsStackSlot()) << destination;
4163 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4164 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004165 } else if (constant->IsLongConstant()) {
4166 int64_t value = constant->AsLongConstant()->GetValue();
4167 int32_t low_value = Low32Bits(value);
4168 int32_t high_value = High32Bits(value);
4169 Immediate low(low_value);
4170 Immediate high(high_value);
4171 if (destination.IsDoubleStackSlot()) {
4172 __ movl(Address(ESP, destination.GetStackIndex()), low);
4173 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4174 } else {
4175 __ movl(destination.AsRegisterPairLow<Register>(), low);
4176 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4177 }
4178 } else {
4179 DCHECK(constant->IsDoubleConstant());
4180 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004181 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004182 int32_t low_value = Low32Bits(value);
4183 int32_t high_value = High32Bits(value);
4184 Immediate low(low_value);
4185 Immediate high(high_value);
4186 if (destination.IsFpuRegister()) {
4187 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4188 if (value == 0) {
4189 // Easy handling of 0.0.
4190 __ xorpd(dest, dest);
4191 } else {
4192 __ pushl(high);
4193 __ pushl(low);
4194 __ movsd(dest, Address(ESP, 0));
4195 __ addl(ESP, Immediate(8));
4196 }
4197 } else {
4198 DCHECK(destination.IsDoubleStackSlot()) << destination;
4199 __ movl(Address(ESP, destination.GetStackIndex()), low);
4200 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4201 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004202 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004203 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004204 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004205 }
4206}
4207
Mark Mendella5c19ce2015-04-01 12:51:05 -04004208void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004209 Register suggested_scratch = reg == EAX ? EBX : EAX;
4210 ScratchRegisterScope ensure_scratch(
4211 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4212
4213 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4214 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4215 __ movl(Address(ESP, mem + stack_offset), reg);
4216 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004217}
4218
Mark Mendell7c8d0092015-01-26 11:21:33 -05004219void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004220 ScratchRegisterScope ensure_scratch(
4221 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4222
4223 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4224 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4225 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4226 __ movss(Address(ESP, mem + stack_offset), reg);
4227 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004228}
4229
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004230void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004231 ScratchRegisterScope ensure_scratch1(
4232 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004233
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004234 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4235 ScratchRegisterScope ensure_scratch2(
4236 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004237
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004238 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4239 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4240 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4241 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4242 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4243 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004244}
4245
4246void ParallelMoveResolverX86::EmitSwap(size_t index) {
4247 MoveOperands* move = moves_.Get(index);
4248 Location source = move->GetSource();
4249 Location destination = move->GetDestination();
4250
4251 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004252 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004253 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004254 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004255 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004256 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004257 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4258 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004259 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4260 // Use XOR Swap algorithm to avoid a temporary.
4261 DCHECK_NE(source.reg(), destination.reg());
4262 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4263 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4264 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4265 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4266 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4267 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4268 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004269 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4270 // Take advantage of the 16 bytes in the XMM register.
4271 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4272 Address stack(ESP, destination.GetStackIndex());
4273 // Load the double into the high doubleword.
4274 __ movhpd(reg, stack);
4275
4276 // Store the low double into the destination.
4277 __ movsd(stack, reg);
4278
4279 // Move the high double to the low double.
4280 __ psrldq(reg, Immediate(8));
4281 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4282 // Take advantage of the 16 bytes in the XMM register.
4283 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4284 Address stack(ESP, source.GetStackIndex());
4285 // Load the double into the high doubleword.
4286 __ movhpd(reg, stack);
4287
4288 // Store the low double into the destination.
4289 __ movsd(stack, reg);
4290
4291 // Move the high double to the low double.
4292 __ psrldq(reg, Immediate(8));
4293 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4294 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4295 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004296 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004297 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004298 }
4299}
4300
4301void ParallelMoveResolverX86::SpillScratch(int reg) {
4302 __ pushl(static_cast<Register>(reg));
4303}
4304
4305void ParallelMoveResolverX86::RestoreScratch(int reg) {
4306 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004307}
4308
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004309void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004310 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4311 ? LocationSummary::kCallOnSlowPath
4312 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004313 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004314 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004315 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004316 locations->SetOut(Location::RequiresRegister());
4317}
4318
4319void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004320 LocationSummary* locations = cls->GetLocations();
4321 Register out = locations->Out().AsRegister<Register>();
4322 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004323 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004324 DCHECK(!cls->CanCallRuntime());
4325 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004326 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004327 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004328 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004329 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004330 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004331 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004332
4333 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4334 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4335 codegen_->AddSlowPath(slow_path);
4336 __ testl(out, out);
4337 __ j(kEqual, slow_path->GetEntryLabel());
4338 if (cls->MustGenerateClinitCheck()) {
4339 GenerateClassInitializationCheck(slow_path, out);
4340 } else {
4341 __ Bind(slow_path->GetExitLabel());
4342 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004343 }
4344}
4345
4346void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4347 LocationSummary* locations =
4348 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4349 locations->SetInAt(0, Location::RequiresRegister());
4350 if (check->HasUses()) {
4351 locations->SetOut(Location::SameAsFirstInput());
4352 }
4353}
4354
4355void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004356 // We assume the class to not be null.
4357 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4358 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004359 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004360 GenerateClassInitializationCheck(slow_path,
4361 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004362}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004363
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004364void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4365 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004366 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4367 Immediate(mirror::Class::kStatusInitialized));
4368 __ j(kLess, slow_path->GetEntryLabel());
4369 __ Bind(slow_path->GetExitLabel());
4370 // No need for memory fence, thanks to the X86 memory model.
4371}
4372
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004373void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4374 LocationSummary* locations =
4375 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004376 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004377 locations->SetOut(Location::RequiresRegister());
4378}
4379
4380void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4381 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4382 codegen_->AddSlowPath(slow_path);
4383
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004384 LocationSummary* locations = load->GetLocations();
4385 Register out = locations->Out().AsRegister<Register>();
4386 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004387 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004388 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004389 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4390 __ testl(out, out);
4391 __ j(kEqual, slow_path->GetEntryLabel());
4392 __ Bind(slow_path->GetExitLabel());
4393}
4394
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004395void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4396 LocationSummary* locations =
4397 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4398 locations->SetOut(Location::RequiresRegister());
4399}
4400
4401void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4402 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004403 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004404 __ fs()->movl(address, Immediate(0));
4405}
4406
4407void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4408 LocationSummary* locations =
4409 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4410 InvokeRuntimeCallingConvention calling_convention;
4411 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4412}
4413
4414void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4415 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4416 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4417}
4418
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004419void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004420 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4421 ? LocationSummary::kNoCall
4422 : LocationSummary::kCallOnSlowPath;
4423 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4424 locations->SetInAt(0, Location::RequiresRegister());
4425 locations->SetInAt(1, Location::Any());
4426 locations->SetOut(Location::RequiresRegister());
4427}
4428
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004429void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004430 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004431 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004432 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004433 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004434 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4435 Label done, zero;
4436 SlowPathCodeX86* slow_path = nullptr;
4437
4438 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004439 // Avoid null check if we know obj is not null.
4440 if (instruction->MustDoNullCheck()) {
4441 __ testl(obj, obj);
4442 __ j(kEqual, &zero);
4443 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004444 __ movl(out, Address(obj, class_offset));
4445 // Compare the class of `obj` with `cls`.
4446 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004447 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004448 } else {
4449 DCHECK(cls.IsStackSlot()) << cls;
4450 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4451 }
4452
4453 if (instruction->IsClassFinal()) {
4454 // Classes must be equal for the instanceof to succeed.
4455 __ j(kNotEqual, &zero);
4456 __ movl(out, Immediate(1));
4457 __ jmp(&done);
4458 } else {
4459 // If the classes are not equal, we go into a slow path.
4460 DCHECK(locations->OnlyCallsOnSlowPath());
4461 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004462 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004463 codegen_->AddSlowPath(slow_path);
4464 __ j(kNotEqual, slow_path->GetEntryLabel());
4465 __ movl(out, Immediate(1));
4466 __ jmp(&done);
4467 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004468
4469 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4470 __ Bind(&zero);
4471 __ movl(out, Immediate(0));
4472 }
4473
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004474 if (slow_path != nullptr) {
4475 __ Bind(slow_path->GetExitLabel());
4476 }
4477 __ Bind(&done);
4478}
4479
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004480void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4481 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4482 instruction, LocationSummary::kCallOnSlowPath);
4483 locations->SetInAt(0, Location::RequiresRegister());
4484 locations->SetInAt(1, Location::Any());
4485 locations->AddTemp(Location::RequiresRegister());
4486}
4487
4488void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4489 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004490 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004491 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004492 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004493 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4494 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4495 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4496 codegen_->AddSlowPath(slow_path);
4497
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004498 // Avoid null check if we know obj is not null.
4499 if (instruction->MustDoNullCheck()) {
4500 __ testl(obj, obj);
4501 __ j(kEqual, slow_path->GetExitLabel());
4502 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004503
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004504 __ movl(temp, Address(obj, class_offset));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004505 // Compare the class of `obj` with `cls`.
4506 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004507 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004508 } else {
4509 DCHECK(cls.IsStackSlot()) << cls;
4510 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4511 }
4512
4513 __ j(kNotEqual, slow_path->GetEntryLabel());
4514 __ Bind(slow_path->GetExitLabel());
4515}
4516
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004517void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4518 LocationSummary* locations =
4519 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4520 InvokeRuntimeCallingConvention calling_convention;
4521 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4522}
4523
4524void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4525 __ fs()->call(Address::Absolute(instruction->IsEnter()
4526 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4527 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4528 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4529}
4530
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004531void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4532void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4533void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4534
4535void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4536 LocationSummary* locations =
4537 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4538 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4539 || instruction->GetResultType() == Primitive::kPrimLong);
4540 locations->SetInAt(0, Location::RequiresRegister());
4541 locations->SetInAt(1, Location::Any());
4542 locations->SetOut(Location::SameAsFirstInput());
4543}
4544
4545void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4546 HandleBitwiseOperation(instruction);
4547}
4548
4549void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4550 HandleBitwiseOperation(instruction);
4551}
4552
4553void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4554 HandleBitwiseOperation(instruction);
4555}
4556
4557void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4558 LocationSummary* locations = instruction->GetLocations();
4559 Location first = locations->InAt(0);
4560 Location second = locations->InAt(1);
4561 DCHECK(first.Equals(locations->Out()));
4562
4563 if (instruction->GetResultType() == Primitive::kPrimInt) {
4564 if (second.IsRegister()) {
4565 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004566 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004567 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004568 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004569 } else {
4570 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004571 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004572 }
4573 } else if (second.IsConstant()) {
4574 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004575 __ andl(first.AsRegister<Register>(),
4576 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004577 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004578 __ orl(first.AsRegister<Register>(),
4579 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004580 } else {
4581 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004582 __ xorl(first.AsRegister<Register>(),
4583 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004584 }
4585 } else {
4586 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004587 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004588 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004589 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004590 } else {
4591 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004592 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004593 }
4594 }
4595 } else {
4596 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4597 if (second.IsRegisterPair()) {
4598 if (instruction->IsAnd()) {
4599 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4600 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4601 } else if (instruction->IsOr()) {
4602 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4603 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4604 } else {
4605 DCHECK(instruction->IsXor());
4606 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4607 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4608 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004609 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004610 if (instruction->IsAnd()) {
4611 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4612 __ andl(first.AsRegisterPairHigh<Register>(),
4613 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4614 } else if (instruction->IsOr()) {
4615 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4616 __ orl(first.AsRegisterPairHigh<Register>(),
4617 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4618 } else {
4619 DCHECK(instruction->IsXor());
4620 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4621 __ xorl(first.AsRegisterPairHigh<Register>(),
4622 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4623 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004624 } else {
4625 DCHECK(second.IsConstant()) << second;
4626 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004627 int32_t low_value = Low32Bits(value);
4628 int32_t high_value = High32Bits(value);
4629 Immediate low(low_value);
4630 Immediate high(high_value);
4631 Register first_low = first.AsRegisterPairLow<Register>();
4632 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004633 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004634 if (low_value == 0) {
4635 __ xorl(first_low, first_low);
4636 } else if (low_value != -1) {
4637 __ andl(first_low, low);
4638 }
4639 if (high_value == 0) {
4640 __ xorl(first_high, first_high);
4641 } else if (high_value != -1) {
4642 __ andl(first_high, high);
4643 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004644 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004645 if (low_value != 0) {
4646 __ orl(first_low, low);
4647 }
4648 if (high_value != 0) {
4649 __ orl(first_high, high);
4650 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004651 } else {
4652 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004653 if (low_value != 0) {
4654 __ xorl(first_low, low);
4655 }
4656 if (high_value != 0) {
4657 __ xorl(first_high, high);
4658 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004659 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004660 }
4661 }
4662}
4663
Calin Juravleb1498f62015-02-16 13:13:29 +00004664void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4665 // Nothing to do, this should be removed during prepare for register allocator.
4666 UNUSED(instruction);
4667 LOG(FATAL) << "Unreachable";
4668}
4669
4670void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4671 // Nothing to do, this should be removed during prepare for register allocator.
4672 UNUSED(instruction);
4673 LOG(FATAL) << "Unreachable";
4674}
4675
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004676} // namespace x86
4677} // namespace art