blob: 236dea1bbac9b28489053de33ce2934b0fd6bbb2 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040025#include "intrinsics.h"
26#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010029#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010033#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace x86 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050044static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045
Mark Mendell24f2dfa2015-01-14 19:51:45 -050046static constexpr int kC2ConditionMask = 0x400;
47
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000048static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000049
Roland Levillain62a46b22015-06-01 18:24:13 +010050#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Calin Juravle175dc732015-08-25 15:42:32 +010051#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052
Andreas Gampe85b62f22015-09-09 13:15:38 -070053class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010055 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056
Alexandre Rames2ed20af2015-03-06 13:55:35 +000057 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010058 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010059 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000060 if (instruction_->CanThrowIntoCatchBlock()) {
61 // Live registers will be restored in the catch block if caught.
62 SaveLiveRegisters(codegen, instruction_->GetLocations());
63 }
Alexandre Rames8158f282015-08-07 10:26:17 +010064 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
65 instruction_,
66 instruction_->GetDexPc(),
67 this);
Roland Levillain888d0672015-11-23 18:53:50 +000068 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010069 }
70
Alexandre Rames8158f282015-08-07 10:26:17 +010071 bool IsFatal() const OVERRIDE { return true; }
72
Alexandre Rames9931f312015-06-19 14:47:01 +010073 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
74
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010076 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
78};
79
Andreas Gampe85b62f22015-09-09 13:15:38 -070080class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000081 public:
82 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
83
Alexandre Rames2ed20af2015-03-06 13:55:35 +000084 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010085 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000086 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000087 if (instruction_->CanThrowIntoCatchBlock()) {
88 // Live registers will be restored in the catch block if caught.
89 SaveLiveRegisters(codegen, instruction_->GetLocations());
90 }
Alexandre Rames8158f282015-08-07 10:26:17 +010091 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
92 instruction_,
93 instruction_->GetDexPc(),
94 this);
Roland Levillain888d0672015-11-23 18:53:50 +000095 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000096 }
97
Alexandre Rames8158f282015-08-07 10:26:17 +010098 bool IsFatal() const OVERRIDE { return true; }
99
Alexandre Rames9931f312015-06-19 14:47:01 +0100100 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
101
Calin Juravled0d48522014-11-04 16:40:20 +0000102 private:
103 HDivZeroCheck* const instruction_;
104 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
105};
106
Andreas Gampe85b62f22015-09-09 13:15:38 -0700107class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000108 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100109 DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000110
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000111 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000112 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(reg_);
115 } else {
116 __ movl(reg_, Immediate(0));
117 }
Calin Juravled0d48522014-11-04 16:40:20 +0000118 __ jmp(GetExitLabel());
119 }
120
Alexandre Rames9931f312015-06-19 14:47:01 +0100121 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
122
Calin Juravled0d48522014-11-04 16:40:20 +0000123 private:
124 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 bool is_div_;
126 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000127};
128
Andreas Gampe85b62f22015-09-09 13:15:38 -0700129class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100130 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100131 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100132
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000133 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100134 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100135 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100136 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000137 // We're moving two locations to locations that could overlap, so we need a parallel
138 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000139 if (instruction_->CanThrowIntoCatchBlock()) {
140 // Live registers will be restored in the catch block if caught.
141 SaveLiveRegisters(codegen, instruction_->GetLocations());
142 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100143 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000144 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100145 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000146 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100147 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100148 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100149 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
150 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100151 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
152 instruction_,
153 instruction_->GetDexPc(),
154 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000155 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100156 }
157
Alexandre Rames8158f282015-08-07 10:26:17 +0100158 bool IsFatal() const OVERRIDE { return true; }
159
Alexandre Rames9931f312015-06-19 14:47:01 +0100160 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
161
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100162 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100163 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100164
165 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
166};
167
Andreas Gampe85b62f22015-09-09 13:15:38 -0700168class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000169 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000170 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100171 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000172
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000173 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100174 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000175 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000176 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100177 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
178 instruction_,
179 instruction_->GetDexPc(),
180 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000181 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000182 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100183 if (successor_ == nullptr) {
184 __ jmp(GetReturnLabel());
185 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100186 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100187 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000188 }
189
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100190 Label* GetReturnLabel() {
191 DCHECK(successor_ == nullptr);
192 return &return_label_;
193 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000194
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100195 HBasicBlock* GetSuccessor() const {
196 return successor_;
197 }
198
Alexandre Rames9931f312015-06-19 14:47:01 +0100199 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
200
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000201 private:
202 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100203 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000204 Label return_label_;
205
206 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
207};
208
Andreas Gampe85b62f22015-09-09 13:15:38 -0700209class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000210 public:
211 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
212
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000213 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000214 LocationSummary* locations = instruction_->GetLocations();
215 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
216
217 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
218 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000219 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000220
221 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800222 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100223 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
224 instruction_,
225 instruction_->GetDexPc(),
226 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000227 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000228 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000229 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000230
231 __ jmp(GetExitLabel());
232 }
233
Alexandre Rames9931f312015-06-19 14:47:01 +0100234 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
235
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000236 private:
237 HLoadString* const instruction_;
238
239 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
240};
241
Andreas Gampe85b62f22015-09-09 13:15:38 -0700242class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000243 public:
244 LoadClassSlowPathX86(HLoadClass* cls,
245 HInstruction* at,
246 uint32_t dex_pc,
247 bool do_clinit)
248 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
249 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
250 }
251
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000252 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000253 LocationSummary* locations = at_->GetLocations();
254 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
255 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000256 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000257
258 InvokeRuntimeCallingConvention calling_convention;
259 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100260 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
261 : QUICK_ENTRY_POINT(pInitializeType),
262 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000263 if (do_clinit_) {
264 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
265 } else {
266 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
267 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000268
269 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000270 Location out = locations->Out();
271 if (out.IsValid()) {
272 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
273 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000275
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000276 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000277 __ jmp(GetExitLabel());
278 }
279
Alexandre Rames9931f312015-06-19 14:47:01 +0100280 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
281
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000282 private:
283 // The class this slow path will load.
284 HLoadClass* const cls_;
285
286 // The instruction where this slow path is happening.
287 // (Might be the load class or an initialization check).
288 HInstruction* const at_;
289
290 // The dex PC of `at_`.
291 const uint32_t dex_pc_;
292
293 // Whether to initialize the class.
294 const bool do_clinit_;
295
296 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
297};
298
Andreas Gampe85b62f22015-09-09 13:15:38 -0700299class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000301 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
302 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000303
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000304 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100306 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
307 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000308 DCHECK(instruction_->IsCheckCast()
309 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310
311 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
312 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000313
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000314 if (!is_fatal_) {
315 SaveLiveRegisters(codegen, locations);
316 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000317
318 // We're moving two locations to locations that could overlap, so we need a parallel
319 // move resolver.
320 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000321 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100322 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000323 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100324 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100325 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100326 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
327 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000329 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100330 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
331 instruction_,
332 instruction_->GetDexPc(),
333 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000334 CheckEntrypointTypes<
335 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000336 } else {
337 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100338 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
339 instruction_,
340 instruction_->GetDexPc(),
341 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000342 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000343 }
344
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000345 if (!is_fatal_) {
346 if (instruction_->IsInstanceOf()) {
347 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
348 }
349 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000350
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000351 __ jmp(GetExitLabel());
352 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000353 }
354
Alexandre Rames9931f312015-06-19 14:47:01 +0100355 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000356 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100357
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000358 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000359 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000360 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000361
362 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
363};
364
Andreas Gampe85b62f22015-09-09 13:15:38 -0700365class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700366 public:
Aart Bik42249c32016-01-07 15:33:50 -0800367 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700368 : instruction_(instruction) {}
369
370 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100371 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700372 __ Bind(GetEntryLabel());
373 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100374 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
375 instruction_,
376 instruction_->GetDexPc(),
377 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000378 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700379 }
380
Alexandre Rames9931f312015-06-19 14:47:01 +0100381 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
382
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700383 private:
Aart Bik42249c32016-01-07 15:33:50 -0800384 HDeoptimize* const instruction_;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700385 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
386};
387
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100388class ArraySetSlowPathX86 : public SlowPathCode {
389 public:
390 explicit ArraySetSlowPathX86(HInstruction* instruction) : instruction_(instruction) {}
391
392 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
393 LocationSummary* locations = instruction_->GetLocations();
394 __ Bind(GetEntryLabel());
395 SaveLiveRegisters(codegen, locations);
396
397 InvokeRuntimeCallingConvention calling_convention;
398 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
399 parallel_move.AddMove(
400 locations->InAt(0),
401 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
402 Primitive::kPrimNot,
403 nullptr);
404 parallel_move.AddMove(
405 locations->InAt(1),
406 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
407 Primitive::kPrimInt,
408 nullptr);
409 parallel_move.AddMove(
410 locations->InAt(2),
411 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
412 Primitive::kPrimNot,
413 nullptr);
414 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
415
416 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
417 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
418 instruction_,
419 instruction_->GetDexPc(),
420 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000421 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100422 RestoreLiveRegisters(codegen, locations);
423 __ jmp(GetExitLabel());
424 }
425
426 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
427
428 private:
429 HInstruction* const instruction_;
430
431 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
432};
433
Roland Levillain7c1559a2015-12-15 10:55:36 +0000434// Slow path marking an object during a read barrier.
435class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
436 public:
437 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location out, Location obj)
438 : instruction_(instruction), out_(out), obj_(obj) {
439 DCHECK(kEmitCompilerReadBarrier);
440 }
441
442 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
443
444 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
445 LocationSummary* locations = instruction_->GetLocations();
446 Register reg_out = out_.AsRegister<Register>();
447 DCHECK(locations->CanCall());
448 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
449 DCHECK(instruction_->IsInstanceFieldGet() ||
450 instruction_->IsStaticFieldGet() ||
451 instruction_->IsArrayGet() ||
452 instruction_->IsLoadClass() ||
453 instruction_->IsLoadString() ||
454 instruction_->IsInstanceOf() ||
455 instruction_->IsCheckCast())
456 << "Unexpected instruction in read barrier marking slow path: "
457 << instruction_->DebugName();
458
459 __ Bind(GetEntryLabel());
460 SaveLiveRegisters(codegen, locations);
461
462 InvokeRuntimeCallingConvention calling_convention;
463 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
464 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
465 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
466 instruction_,
467 instruction_->GetDexPc(),
468 this);
469 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
470 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
471
472 RestoreLiveRegisters(codegen, locations);
473 __ jmp(GetExitLabel());
474 }
475
476 private:
477 HInstruction* const instruction_;
478 const Location out_;
479 const Location obj_;
480
481 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
482};
483
Roland Levillain0d5a2812015-11-13 10:07:31 +0000484// Slow path generating a read barrier for a heap reference.
485class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
486 public:
487 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
488 Location out,
489 Location ref,
490 Location obj,
491 uint32_t offset,
492 Location index)
493 : instruction_(instruction),
494 out_(out),
495 ref_(ref),
496 obj_(obj),
497 offset_(offset),
498 index_(index) {
499 DCHECK(kEmitCompilerReadBarrier);
500 // If `obj` is equal to `out` or `ref`, it means the initial object
501 // has been overwritten by (or after) the heap object reference load
502 // to be instrumented, e.g.:
503 //
504 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000505 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000506 //
507 // In that case, we have lost the information about the original
508 // object, and the emitted read barrier cannot work properly.
509 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
510 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
511 }
512
513 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
514 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
515 LocationSummary* locations = instruction_->GetLocations();
516 Register reg_out = out_.AsRegister<Register>();
517 DCHECK(locations->CanCall());
518 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
519 DCHECK(!instruction_->IsInvoke() ||
520 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000521 instruction_->GetLocations()->Intrinsified()))
522 << "Unexpected instruction in read barrier for heap reference slow path: "
523 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000524
525 __ Bind(GetEntryLabel());
526 SaveLiveRegisters(codegen, locations);
527
528 // We may have to change the index's value, but as `index_` is a
529 // constant member (like other "inputs" of this slow path),
530 // introduce a copy of it, `index`.
531 Location index = index_;
532 if (index_.IsValid()) {
533 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
534 if (instruction_->IsArrayGet()) {
535 // Compute the actual memory offset and store it in `index`.
536 Register index_reg = index_.AsRegister<Register>();
537 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
538 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
539 // We are about to change the value of `index_reg` (see the
540 // calls to art::x86::X86Assembler::shll and
541 // art::x86::X86Assembler::AddImmediate below), but it has
542 // not been saved by the previous call to
543 // art::SlowPathCode::SaveLiveRegisters, as it is a
544 // callee-save register --
545 // art::SlowPathCode::SaveLiveRegisters does not consider
546 // callee-save registers, as it has been designed with the
547 // assumption that callee-save registers are supposed to be
548 // handled by the called function. So, as a callee-save
549 // register, `index_reg` _would_ eventually be saved onto
550 // the stack, but it would be too late: we would have
551 // changed its value earlier. Therefore, we manually save
552 // it here into another freely available register,
553 // `free_reg`, chosen of course among the caller-save
554 // registers (as a callee-save `free_reg` register would
555 // exhibit the same problem).
556 //
557 // Note we could have requested a temporary register from
558 // the register allocator instead; but we prefer not to, as
559 // this is a slow path, and we know we can find a
560 // caller-save register that is available.
561 Register free_reg = FindAvailableCallerSaveRegister(codegen);
562 __ movl(free_reg, index_reg);
563 index_reg = free_reg;
564 index = Location::RegisterLocation(index_reg);
565 } else {
566 // The initial register stored in `index_` has already been
567 // saved in the call to art::SlowPathCode::SaveLiveRegisters
568 // (as it is not a callee-save register), so we can freely
569 // use it.
570 }
571 // Shifting the index value contained in `index_reg` by the scale
572 // factor (2) cannot overflow in practice, as the runtime is
573 // unable to allocate object arrays with a size larger than
574 // 2^26 - 1 (that is, 2^28 - 4 bytes).
575 __ shll(index_reg, Immediate(TIMES_4));
576 static_assert(
577 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
578 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
579 __ AddImmediate(index_reg, Immediate(offset_));
580 } else {
581 DCHECK(instruction_->IsInvoke());
582 DCHECK(instruction_->GetLocations()->Intrinsified());
583 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
584 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
585 << instruction_->AsInvoke()->GetIntrinsic();
586 DCHECK_EQ(offset_, 0U);
587 DCHECK(index_.IsRegisterPair());
588 // UnsafeGet's offset location is a register pair, the low
589 // part contains the correct offset.
590 index = index_.ToLow();
591 }
592 }
593
594 // We're moving two or three locations to locations that could
595 // overlap, so we need a parallel move resolver.
596 InvokeRuntimeCallingConvention calling_convention;
597 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
598 parallel_move.AddMove(ref_,
599 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
600 Primitive::kPrimNot,
601 nullptr);
602 parallel_move.AddMove(obj_,
603 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
604 Primitive::kPrimNot,
605 nullptr);
606 if (index.IsValid()) {
607 parallel_move.AddMove(index,
608 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
609 Primitive::kPrimInt,
610 nullptr);
611 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
612 } else {
613 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
614 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
615 }
616 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
617 instruction_,
618 instruction_->GetDexPc(),
619 this);
620 CheckEntrypointTypes<
621 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
622 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
623
624 RestoreLiveRegisters(codegen, locations);
625 __ jmp(GetExitLabel());
626 }
627
628 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
629
630 private:
631 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
632 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
633 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
634 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
635 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
636 return static_cast<Register>(i);
637 }
638 }
639 // We shall never fail to find a free caller-save register, as
640 // there are more than two core caller-save registers on x86
641 // (meaning it is possible to find one which is different from
642 // `ref` and `obj`).
643 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
644 LOG(FATAL) << "Could not find a free caller-save register";
645 UNREACHABLE();
646 }
647
648 HInstruction* const instruction_;
649 const Location out_;
650 const Location ref_;
651 const Location obj_;
652 const uint32_t offset_;
653 // An additional location containing an index to an array.
654 // Only used for HArrayGet and the UnsafeGetObject &
655 // UnsafeGetObjectVolatile intrinsics.
656 const Location index_;
657
658 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
659};
660
661// Slow path generating a read barrier for a GC root.
662class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
663 public:
664 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
Roland Levillain7c1559a2015-12-15 10:55:36 +0000665 : instruction_(instruction), out_(out), root_(root) {
666 DCHECK(kEmitCompilerReadBarrier);
667 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000668
669 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
670 LocationSummary* locations = instruction_->GetLocations();
671 Register reg_out = out_.AsRegister<Register>();
672 DCHECK(locations->CanCall());
673 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000674 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
675 << "Unexpected instruction in read barrier for GC root slow path: "
676 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000677
678 __ Bind(GetEntryLabel());
679 SaveLiveRegisters(codegen, locations);
680
681 InvokeRuntimeCallingConvention calling_convention;
682 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
683 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
684 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
685 instruction_,
686 instruction_->GetDexPc(),
687 this);
688 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
689 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
690
691 RestoreLiveRegisters(codegen, locations);
692 __ jmp(GetExitLabel());
693 }
694
695 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
696
697 private:
698 HInstruction* const instruction_;
699 const Location out_;
700 const Location root_;
701
702 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
703};
704
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100705#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100706#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100707
Aart Bike9f37602015-10-09 11:15:55 -0700708inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700709 switch (cond) {
710 case kCondEQ: return kEqual;
711 case kCondNE: return kNotEqual;
712 case kCondLT: return kLess;
713 case kCondLE: return kLessEqual;
714 case kCondGT: return kGreater;
715 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700716 case kCondB: return kBelow;
717 case kCondBE: return kBelowEqual;
718 case kCondA: return kAbove;
719 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700720 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100721 LOG(FATAL) << "Unreachable";
722 UNREACHABLE();
723}
724
Aart Bike9f37602015-10-09 11:15:55 -0700725// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100726inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
727 switch (cond) {
728 case kCondEQ: return kEqual;
729 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700730 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100731 case kCondLT: return kBelow;
732 case kCondLE: return kBelowEqual;
733 case kCondGT: return kAbove;
734 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700735 // Unsigned remain unchanged.
736 case kCondB: return kBelow;
737 case kCondBE: return kBelowEqual;
738 case kCondA: return kAbove;
739 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100740 }
741 LOG(FATAL) << "Unreachable";
742 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700743}
744
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100745void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100746 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100747}
748
749void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100750 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100751}
752
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100753size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
754 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
755 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100756}
757
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100758size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
759 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
760 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100761}
762
Mark Mendell7c8d0092015-01-26 11:21:33 -0500763size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
764 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
765 return GetFloatingPointSpillSlotSize();
766}
767
768size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
769 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
770 return GetFloatingPointSpillSlotSize();
771}
772
Calin Juravle175dc732015-08-25 15:42:32 +0100773void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
774 HInstruction* instruction,
775 uint32_t dex_pc,
776 SlowPathCode* slow_path) {
777 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
778 instruction,
779 dex_pc,
780 slow_path);
781}
782
783void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100784 HInstruction* instruction,
785 uint32_t dex_pc,
786 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100787 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100788 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100789 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100790}
791
Mark Mendellfb8d2792015-03-31 22:16:59 -0400792CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000793 const X86InstructionSetFeatures& isa_features,
794 const CompilerOptions& compiler_options,
795 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500796 : CodeGenerator(graph,
797 kNumberOfCpuRegisters,
798 kNumberOfXmmRegisters,
799 kNumberOfRegisterPairs,
800 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
801 arraysize(kCoreCalleeSaves))
802 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100803 0,
804 compiler_options,
805 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100806 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100807 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100808 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400809 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000810 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100811 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400812 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000813 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400814 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000815 // Use a fake return address register to mimic Quick.
816 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100817}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100818
David Brazdil58282f42016-01-14 12:45:10 +0000819void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100820 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100821 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100822
823 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100824 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100825
Calin Juravle34bacdf2014-10-07 20:23:36 +0100826 UpdateBlockedPairRegisters();
827}
828
829void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
830 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
831 X86ManagedRegister current =
832 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
833 if (blocked_core_registers_[current.AsRegisterPairLow()]
834 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
835 blocked_register_pairs_[i] = true;
836 }
837 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100838}
839
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100840InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800841 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100842 assembler_(codegen->GetAssembler()),
843 codegen_(codegen) {}
844
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100845static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100846 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100847}
848
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000849void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100850 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000851 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000852 bool skip_overflow_check =
853 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000854 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000855
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000856 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100857 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100858 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100859 }
860
Mark Mendell5f874182015-03-04 15:42:45 -0500861 if (HasEmptyFrame()) {
862 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000863 }
Mark Mendell5f874182015-03-04 15:42:45 -0500864
865 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
866 Register reg = kCoreCalleeSaves[i];
867 if (allocated_registers_.ContainsCoreRegister(reg)) {
868 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100869 __ cfi().AdjustCFAOffset(kX86WordSize);
870 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500871 }
872 }
873
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100874 int adjust = GetFrameSize() - FrameEntrySpillSize();
875 __ subl(ESP, Immediate(adjust));
876 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100877 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000878}
879
880void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100881 __ cfi().RememberState();
882 if (!HasEmptyFrame()) {
883 int adjust = GetFrameSize() - FrameEntrySpillSize();
884 __ addl(ESP, Immediate(adjust));
885 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500886
David Srbeckyc34dc932015-04-12 09:27:43 +0100887 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
888 Register reg = kCoreCalleeSaves[i];
889 if (allocated_registers_.ContainsCoreRegister(reg)) {
890 __ popl(reg);
891 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
892 __ cfi().Restore(DWARFReg(reg));
893 }
Mark Mendell5f874182015-03-04 15:42:45 -0500894 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000895 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100896 __ ret();
897 __ cfi().RestoreState();
898 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000899}
900
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100901void CodeGeneratorX86::Bind(HBasicBlock* block) {
902 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000903}
904
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100905Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
906 switch (load->GetType()) {
907 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100908 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100909 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100910
911 case Primitive::kPrimInt:
912 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100913 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100914 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100915
916 case Primitive::kPrimBoolean:
917 case Primitive::kPrimByte:
918 case Primitive::kPrimChar:
919 case Primitive::kPrimShort:
920 case Primitive::kPrimVoid:
921 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700922 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100923 }
924
925 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700926 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100927}
928
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100929Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
930 switch (type) {
931 case Primitive::kPrimBoolean:
932 case Primitive::kPrimByte:
933 case Primitive::kPrimChar:
934 case Primitive::kPrimShort:
935 case Primitive::kPrimInt:
936 case Primitive::kPrimNot:
937 return Location::RegisterLocation(EAX);
938
939 case Primitive::kPrimLong:
940 return Location::RegisterPairLocation(EAX, EDX);
941
942 case Primitive::kPrimVoid:
943 return Location::NoLocation();
944
945 case Primitive::kPrimDouble:
946 case Primitive::kPrimFloat:
947 return Location::FpuRegisterLocation(XMM0);
948 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100949
950 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100951}
952
953Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
954 return Location::RegisterLocation(kMethodRegisterArgument);
955}
956
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100957Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100958 switch (type) {
959 case Primitive::kPrimBoolean:
960 case Primitive::kPrimByte:
961 case Primitive::kPrimChar:
962 case Primitive::kPrimShort:
963 case Primitive::kPrimInt:
964 case Primitive::kPrimNot: {
965 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000966 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100967 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100968 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100969 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000970 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100971 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100972 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100973
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000974 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100975 uint32_t index = gp_index_;
976 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000977 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100978 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100979 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
980 calling_convention.GetRegisterPairAt(index));
981 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100982 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000983 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
984 }
985 }
986
987 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100988 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000989 stack_index_++;
990 if (index < calling_convention.GetNumberOfFpuRegisters()) {
991 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
992 } else {
993 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
994 }
995 }
996
997 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100998 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000999 stack_index_ += 2;
1000 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1001 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1002 } else {
1003 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001004 }
1005 }
1006
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001007 case Primitive::kPrimVoid:
1008 LOG(FATAL) << "Unexpected parameter type " << type;
1009 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001010 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001011 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001012}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001013
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001014void CodeGeneratorX86::Move32(Location destination, Location source) {
1015 if (source.Equals(destination)) {
1016 return;
1017 }
1018 if (destination.IsRegister()) {
1019 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001020 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001021 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001022 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001023 } else {
1024 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001025 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001026 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001027 } else if (destination.IsFpuRegister()) {
1028 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001029 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001030 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001031 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001032 } else {
1033 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001034 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001035 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001037 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001038 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001039 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001040 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001041 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001042 } else if (source.IsConstant()) {
1043 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001044 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001045 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001046 } else {
1047 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001048 __ pushl(Address(ESP, source.GetStackIndex()));
1049 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001050 }
1051 }
1052}
1053
1054void CodeGeneratorX86::Move64(Location destination, Location source) {
1055 if (source.Equals(destination)) {
1056 return;
1057 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001058 if (destination.IsRegisterPair()) {
1059 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001060 EmitParallelMoves(
1061 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1062 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001063 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001064 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001065 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1066 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001067 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001068 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1069 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1070 __ psrlq(src_reg, Immediate(32));
1071 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001073 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001074 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001075 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1076 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001077 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1078 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001079 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001080 if (source.IsFpuRegister()) {
1081 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1082 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001083 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001084 } else if (source.IsRegisterPair()) {
1085 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1086 // Create stack space for 2 elements.
1087 __ subl(ESP, Immediate(2 * elem_size));
1088 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1089 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1090 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1091 // And remove the temporary stack space we allocated.
1092 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001093 } else {
1094 LOG(FATAL) << "Unimplemented";
1095 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001096 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001097 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001098 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001099 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001100 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001101 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001102 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001103 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001104 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001105 } else if (source.IsConstant()) {
1106 HConstant* constant = source.GetConstant();
1107 int64_t value;
1108 if (constant->IsLongConstant()) {
1109 value = constant->AsLongConstant()->GetValue();
1110 } else {
1111 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001112 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001113 }
1114 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1115 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001116 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001117 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001118 EmitParallelMoves(
1119 Location::StackSlot(source.GetStackIndex()),
1120 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001121 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001122 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001123 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1124 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001125 }
1126 }
1127}
1128
Calin Juravle175dc732015-08-25 15:42:32 +01001129void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1130 DCHECK(location.IsRegister());
1131 __ movl(location.AsRegister<Register>(), Immediate(value));
1132}
1133
Calin Juravlee460d1d2015-09-29 04:52:17 +01001134void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001135 HParallelMove move(GetGraph()->GetArena());
1136 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1137 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1138 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001139 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001140 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001141 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001142 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001143}
1144
1145void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1146 if (location.IsRegister()) {
1147 locations->AddTemp(location);
1148 } else if (location.IsRegisterPair()) {
1149 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1150 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1151 } else {
1152 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1153 }
1154}
1155
David Brazdilfc6a86a2015-06-26 10:33:45 +00001156void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001157 DCHECK(!successor->IsExitBlock());
1158
1159 HBasicBlock* block = got->GetBlock();
1160 HInstruction* previous = got->GetPrevious();
1161
1162 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001163 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001164 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1165 return;
1166 }
1167
1168 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1169 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1170 }
1171 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001172 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001173 }
1174}
1175
David Brazdilfc6a86a2015-06-26 10:33:45 +00001176void LocationsBuilderX86::VisitGoto(HGoto* got) {
1177 got->SetLocations(nullptr);
1178}
1179
1180void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1181 HandleGoto(got, got->GetSuccessor());
1182}
1183
1184void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1185 try_boundary->SetLocations(nullptr);
1186}
1187
1188void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1189 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1190 if (!successor->IsExitBlock()) {
1191 HandleGoto(try_boundary, successor);
1192 }
1193}
1194
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001195void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001196 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001197}
1198
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001199void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001200}
1201
Mark Mendell152408f2015-12-31 12:28:50 -05001202template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001203void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001204 LabelType* true_label,
1205 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001206 if (cond->IsFPConditionTrueIfNaN()) {
1207 __ j(kUnordered, true_label);
1208 } else if (cond->IsFPConditionFalseIfNaN()) {
1209 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001210 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001211 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001212}
1213
Mark Mendell152408f2015-12-31 12:28:50 -05001214template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001215void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001216 LabelType* true_label,
1217 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001218 LocationSummary* locations = cond->GetLocations();
1219 Location left = locations->InAt(0);
1220 Location right = locations->InAt(1);
1221 IfCondition if_cond = cond->GetCondition();
1222
Mark Mendellc4701932015-04-10 13:18:51 -04001223 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001224 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001225 IfCondition true_high_cond = if_cond;
1226 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001227 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001228
1229 // Set the conditions for the test, remembering that == needs to be
1230 // decided using the low words.
1231 switch (if_cond) {
1232 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001233 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001234 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001235 break;
1236 case kCondLT:
1237 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001238 break;
1239 case kCondLE:
1240 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001241 break;
1242 case kCondGT:
1243 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001244 break;
1245 case kCondGE:
1246 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001247 break;
Aart Bike9f37602015-10-09 11:15:55 -07001248 case kCondB:
1249 false_high_cond = kCondA;
1250 break;
1251 case kCondBE:
1252 true_high_cond = kCondB;
1253 break;
1254 case kCondA:
1255 false_high_cond = kCondB;
1256 break;
1257 case kCondAE:
1258 true_high_cond = kCondA;
1259 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001260 }
1261
1262 if (right.IsConstant()) {
1263 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001264 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001265 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001266
Aart Bika19616e2016-02-01 18:57:58 -08001267 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001268 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001269 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001270 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001271 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001272 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001273 __ j(X86Condition(true_high_cond), true_label);
1274 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001275 }
1276 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001277 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001278 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001279 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001280 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001281
1282 __ cmpl(left_high, right_high);
1283 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001284 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001285 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001286 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001287 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001288 __ j(X86Condition(true_high_cond), true_label);
1289 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001290 }
1291 // Must be equal high, so compare the lows.
1292 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001293 } else {
1294 DCHECK(right.IsDoubleStackSlot());
1295 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1296 if (if_cond == kCondNE) {
1297 __ j(X86Condition(true_high_cond), true_label);
1298 } else if (if_cond == kCondEQ) {
1299 __ j(X86Condition(false_high_cond), false_label);
1300 } else {
1301 __ j(X86Condition(true_high_cond), true_label);
1302 __ j(X86Condition(false_high_cond), false_label);
1303 }
1304 // Must be equal high, so compare the lows.
1305 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001306 }
1307 // The last comparison might be unsigned.
1308 __ j(final_condition, true_label);
1309}
1310
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001311void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1312 Location rhs,
1313 HInstruction* insn,
1314 bool is_double) {
1315 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1316 if (is_double) {
1317 if (rhs.IsFpuRegister()) {
1318 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1319 } else if (const_area != nullptr) {
1320 DCHECK(const_area->IsEmittedAtUseSite());
1321 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1322 codegen_->LiteralDoubleAddress(
1323 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1324 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1325 } else {
1326 DCHECK(rhs.IsDoubleStackSlot());
1327 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1328 }
1329 } else {
1330 if (rhs.IsFpuRegister()) {
1331 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1332 } else if (const_area != nullptr) {
1333 DCHECK(const_area->IsEmittedAtUseSite());
1334 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1335 codegen_->LiteralFloatAddress(
1336 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1337 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1338 } else {
1339 DCHECK(rhs.IsStackSlot());
1340 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1341 }
1342 }
1343}
1344
Mark Mendell152408f2015-12-31 12:28:50 -05001345template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001346void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001347 LabelType* true_target_in,
1348 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001349 // Generated branching requires both targets to be explicit. If either of the
1350 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001351 LabelType fallthrough_target;
1352 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1353 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001354
Mark Mendellc4701932015-04-10 13:18:51 -04001355 LocationSummary* locations = condition->GetLocations();
1356 Location left = locations->InAt(0);
1357 Location right = locations->InAt(1);
1358
Mark Mendellc4701932015-04-10 13:18:51 -04001359 Primitive::Type type = condition->InputAt(0)->GetType();
1360 switch (type) {
1361 case Primitive::kPrimLong:
1362 GenerateLongComparesAndJumps(condition, true_target, false_target);
1363 break;
1364 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001365 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001366 GenerateFPJumps(condition, true_target, false_target);
1367 break;
1368 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001369 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001370 GenerateFPJumps(condition, true_target, false_target);
1371 break;
1372 default:
1373 LOG(FATAL) << "Unexpected compare type " << type;
1374 }
1375
David Brazdil0debae72015-11-12 18:37:00 +00001376 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001377 __ jmp(false_target);
1378 }
David Brazdil0debae72015-11-12 18:37:00 +00001379
1380 if (fallthrough_target.IsLinked()) {
1381 __ Bind(&fallthrough_target);
1382 }
Mark Mendellc4701932015-04-10 13:18:51 -04001383}
1384
David Brazdil0debae72015-11-12 18:37:00 +00001385static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1386 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1387 // are set only strictly before `branch`. We can't use the eflags on long/FP
1388 // conditions if they are materialized due to the complex branching.
1389 return cond->IsCondition() &&
1390 cond->GetNext() == branch &&
1391 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1392 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1393}
1394
Mark Mendell152408f2015-12-31 12:28:50 -05001395template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001396void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001397 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001398 LabelType* true_target,
1399 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001400 HInstruction* cond = instruction->InputAt(condition_input_index);
1401
1402 if (true_target == nullptr && false_target == nullptr) {
1403 // Nothing to do. The code always falls through.
1404 return;
1405 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001406 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001407 if (cond->AsIntConstant()->IsOne()) {
1408 if (true_target != nullptr) {
1409 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001410 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001411 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001412 DCHECK(cond->AsIntConstant()->IsZero());
1413 if (false_target != nullptr) {
1414 __ jmp(false_target);
1415 }
1416 }
1417 return;
1418 }
1419
1420 // The following code generates these patterns:
1421 // (1) true_target == nullptr && false_target != nullptr
1422 // - opposite condition true => branch to false_target
1423 // (2) true_target != nullptr && false_target == nullptr
1424 // - condition true => branch to true_target
1425 // (3) true_target != nullptr && false_target != nullptr
1426 // - condition true => branch to true_target
1427 // - branch to false_target
1428 if (IsBooleanValueOrMaterializedCondition(cond)) {
1429 if (AreEflagsSetFrom(cond, instruction)) {
1430 if (true_target == nullptr) {
1431 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1432 } else {
1433 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1434 }
1435 } else {
1436 // Materialized condition, compare against 0.
1437 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1438 if (lhs.IsRegister()) {
1439 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1440 } else {
1441 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1442 }
1443 if (true_target == nullptr) {
1444 __ j(kEqual, false_target);
1445 } else {
1446 __ j(kNotEqual, true_target);
1447 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001448 }
1449 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001450 // Condition has not been materialized, use its inputs as the comparison and
1451 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001452 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001453
1454 // If this is a long or FP comparison that has been folded into
1455 // the HCondition, generate the comparison directly.
1456 Primitive::Type type = condition->InputAt(0)->GetType();
1457 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1458 GenerateCompareTestAndBranch(condition, true_target, false_target);
1459 return;
1460 }
1461
1462 Location lhs = condition->GetLocations()->InAt(0);
1463 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001464 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001465 if (rhs.IsRegister()) {
1466 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1467 } else if (rhs.IsConstant()) {
1468 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001469 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001470 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001471 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1472 }
1473 if (true_target == nullptr) {
1474 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1475 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001476 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001477 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001478 }
David Brazdil0debae72015-11-12 18:37:00 +00001479
1480 // If neither branch falls through (case 3), the conditional branch to `true_target`
1481 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1482 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001483 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001484 }
1485}
1486
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001487void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001488 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1489 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001490 locations->SetInAt(0, Location::Any());
1491 }
1492}
1493
1494void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001495 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1496 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1497 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1498 nullptr : codegen_->GetLabelOf(true_successor);
1499 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1500 nullptr : codegen_->GetLabelOf(false_successor);
1501 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001502}
1503
1504void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1505 LocationSummary* locations = new (GetGraph()->GetArena())
1506 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001507 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001508 locations->SetInAt(0, Location::Any());
1509 }
1510}
1511
1512void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001513 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001514 GenerateTestAndBranch<Label>(deoptimize,
1515 /* condition_input_index */ 0,
1516 slow_path->GetEntryLabel(),
1517 /* false_target */ nullptr);
1518}
1519
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001520static bool SelectCanUseCMOV(HSelect* select) {
1521 // There are no conditional move instructions for XMMs.
1522 if (Primitive::IsFloatingPointType(select->GetType())) {
1523 return false;
1524 }
1525
1526 // A FP condition doesn't generate the single CC that we need.
1527 // In 32 bit mode, a long condition doesn't generate a single CC either.
1528 HInstruction* condition = select->GetCondition();
1529 if (condition->IsCondition()) {
1530 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1531 if (compare_type == Primitive::kPrimLong ||
1532 Primitive::IsFloatingPointType(compare_type)) {
1533 return false;
1534 }
1535 }
1536
1537 // We can generate a CMOV for this Select.
1538 return true;
1539}
1540
David Brazdil74eb1b22015-12-14 11:44:01 +00001541void LocationsBuilderX86::VisitSelect(HSelect* select) {
1542 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001543 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001544 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001545 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001546 } else {
1547 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001548 if (SelectCanUseCMOV(select)) {
1549 if (select->InputAt(1)->IsConstant()) {
1550 // Cmov can't handle a constant value.
1551 locations->SetInAt(1, Location::RequiresRegister());
1552 } else {
1553 locations->SetInAt(1, Location::Any());
1554 }
1555 } else {
1556 locations->SetInAt(1, Location::Any());
1557 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001558 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001559 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1560 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001561 }
1562 locations->SetOut(Location::SameAsFirstInput());
1563}
1564
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001565void InstructionCodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
1566 Register lhs_reg = lhs.AsRegister<Register>();
1567 if (rhs.IsConstant()) {
1568 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1569 codegen_->Compare32BitValue(lhs_reg, value);
1570 } else if (rhs.IsStackSlot()) {
1571 __ cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
1572 } else {
1573 __ cmpl(lhs_reg, rhs.AsRegister<Register>());
1574 }
1575}
1576
David Brazdil74eb1b22015-12-14 11:44:01 +00001577void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1578 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001579 DCHECK(locations->InAt(0).Equals(locations->Out()));
1580 if (SelectCanUseCMOV(select)) {
1581 // If both the condition and the source types are integer, we can generate
1582 // a CMOV to implement Select.
1583
1584 HInstruction* select_condition = select->GetCondition();
1585 Condition cond = kNotEqual;
1586
1587 // Figure out how to test the 'condition'.
1588 if (select_condition->IsCondition()) {
1589 HCondition* condition = select_condition->AsCondition();
1590 if (!condition->IsEmittedAtUseSite()) {
1591 // This was a previously materialized condition.
1592 // Can we use the existing condition code?
1593 if (AreEflagsSetFrom(condition, select)) {
1594 // Materialization was the previous instruction. Condition codes are right.
1595 cond = X86Condition(condition->GetCondition());
1596 } else {
1597 // No, we have to recreate the condition code.
1598 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1599 __ testl(cond_reg, cond_reg);
1600 }
1601 } else {
1602 // We can't handle FP or long here.
1603 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1604 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1605 LocationSummary* cond_locations = condition->GetLocations();
1606 GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
1607 cond = X86Condition(condition->GetCondition());
1608 }
1609 } else {
1610 // Must be a boolean condition, which needs to be compared to 0.
1611 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1612 __ testl(cond_reg, cond_reg);
1613 }
1614
1615 // If the condition is true, overwrite the output, which already contains false.
1616 Location false_loc = locations->InAt(0);
1617 Location true_loc = locations->InAt(1);
1618 if (select->GetType() == Primitive::kPrimLong) {
1619 // 64 bit conditional move.
1620 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1621 Register false_low = false_loc.AsRegisterPairLow<Register>();
1622 if (true_loc.IsRegisterPair()) {
1623 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1624 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1625 } else {
1626 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1627 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1628 }
1629 } else {
1630 // 32 bit conditional move.
1631 Register false_reg = false_loc.AsRegister<Register>();
1632 if (true_loc.IsRegister()) {
1633 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1634 } else {
1635 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1636 }
1637 }
1638 } else {
1639 NearLabel false_target;
1640 GenerateTestAndBranch<NearLabel>(
1641 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1642 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1643 __ Bind(&false_target);
1644 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001645}
1646
David Srbecky0cf44932015-12-09 14:09:59 +00001647void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1648 new (GetGraph()->GetArena()) LocationSummary(info);
1649}
1650
1651void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyc7098ff2016-02-09 14:30:11 +00001652 codegen_->MaybeRecordNativeDebugInfo(info, info->GetDexPc());
1653}
1654
1655void CodeGeneratorX86::GenerateNop() {
1656 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001657}
1658
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001659void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001660 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001661}
1662
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001663void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1664 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001665}
1666
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001667void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001668 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001669}
1670
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001671void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001672 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001673}
1674
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001675void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001676 LocationSummary* locations =
1677 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001678 switch (store->InputAt(1)->GetType()) {
1679 case Primitive::kPrimBoolean:
1680 case Primitive::kPrimByte:
1681 case Primitive::kPrimChar:
1682 case Primitive::kPrimShort:
1683 case Primitive::kPrimInt:
1684 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001685 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001686 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1687 break;
1688
1689 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001690 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001691 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1692 break;
1693
1694 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001695 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001696 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001697}
1698
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001699void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001700}
1701
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001702void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001703 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001704 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001705 // Handle the long/FP comparisons made in instruction simplification.
1706 switch (cond->InputAt(0)->GetType()) {
1707 case Primitive::kPrimLong: {
1708 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001709 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001710 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001711 locations->SetOut(Location::RequiresRegister());
1712 }
1713 break;
1714 }
1715 case Primitive::kPrimFloat:
1716 case Primitive::kPrimDouble: {
1717 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001718 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1719 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1720 } else if (cond->InputAt(1)->IsConstant()) {
1721 locations->SetInAt(1, Location::RequiresFpuRegister());
1722 } else {
1723 locations->SetInAt(1, Location::Any());
1724 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001725 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001726 locations->SetOut(Location::RequiresRegister());
1727 }
1728 break;
1729 }
1730 default:
1731 locations->SetInAt(0, Location::RequiresRegister());
1732 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001733 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001734 // We need a byte register.
1735 locations->SetOut(Location::RegisterLocation(ECX));
1736 }
1737 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001738 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001739}
1740
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001741void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001742 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001743 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001744 }
Mark Mendellc4701932015-04-10 13:18:51 -04001745
1746 LocationSummary* locations = cond->GetLocations();
1747 Location lhs = locations->InAt(0);
1748 Location rhs = locations->InAt(1);
1749 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001750 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001751
1752 switch (cond->InputAt(0)->GetType()) {
1753 default: {
1754 // Integer case.
1755
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001756 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001757 __ xorl(reg, reg);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001758 GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001759 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001760 return;
1761 }
1762 case Primitive::kPrimLong:
1763 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1764 break;
1765 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001766 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001767 GenerateFPJumps(cond, &true_label, &false_label);
1768 break;
1769 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001770 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001771 GenerateFPJumps(cond, &true_label, &false_label);
1772 break;
1773 }
1774
1775 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001776 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001777
Roland Levillain4fa13f62015-07-06 18:11:54 +01001778 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001779 __ Bind(&false_label);
1780 __ xorl(reg, reg);
1781 __ jmp(&done_label);
1782
Roland Levillain4fa13f62015-07-06 18:11:54 +01001783 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001784 __ Bind(&true_label);
1785 __ movl(reg, Immediate(1));
1786 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001787}
1788
1789void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001790 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001791}
1792
1793void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001794 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001795}
1796
1797void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001798 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001799}
1800
1801void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001802 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001803}
1804
1805void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001806 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001807}
1808
1809void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001810 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001811}
1812
1813void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001814 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001815}
1816
1817void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001818 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001819}
1820
1821void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001822 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001823}
1824
1825void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001826 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001827}
1828
1829void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001830 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001831}
1832
1833void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001834 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001835}
1836
Aart Bike9f37602015-10-09 11:15:55 -07001837void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001838 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001839}
1840
1841void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001842 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001843}
1844
1845void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001846 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001847}
1848
1849void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001850 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001851}
1852
1853void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001854 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001855}
1856
1857void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001858 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001859}
1860
1861void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001862 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001863}
1864
1865void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001866 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001867}
1868
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001869void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001870 LocationSummary* locations =
1871 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001872 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001873}
1874
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001875void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001876 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001877}
1878
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001879void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1880 LocationSummary* locations =
1881 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1882 locations->SetOut(Location::ConstantLocation(constant));
1883}
1884
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001885void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001886 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001887}
1888
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001889void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001890 LocationSummary* locations =
1891 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001892 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001893}
1894
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001895void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001896 // Will be generated at use site.
1897}
1898
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001899void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1900 LocationSummary* locations =
1901 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1902 locations->SetOut(Location::ConstantLocation(constant));
1903}
1904
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001905void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001906 // Will be generated at use site.
1907}
1908
1909void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1910 LocationSummary* locations =
1911 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1912 locations->SetOut(Location::ConstantLocation(constant));
1913}
1914
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001915void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001916 // Will be generated at use site.
1917}
1918
Calin Juravle27df7582015-04-17 19:12:31 +01001919void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1920 memory_barrier->SetLocations(nullptr);
1921}
1922
1923void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001924 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001925}
1926
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001927void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001928 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001929}
1930
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001931void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001932 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001933}
1934
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001935void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001936 LocationSummary* locations =
1937 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001938 switch (ret->InputAt(0)->GetType()) {
1939 case Primitive::kPrimBoolean:
1940 case Primitive::kPrimByte:
1941 case Primitive::kPrimChar:
1942 case Primitive::kPrimShort:
1943 case Primitive::kPrimInt:
1944 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001945 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001946 break;
1947
1948 case Primitive::kPrimLong:
1949 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001950 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001951 break;
1952
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001953 case Primitive::kPrimFloat:
1954 case Primitive::kPrimDouble:
1955 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001956 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001957 break;
1958
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001959 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001960 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001961 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001962}
1963
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001964void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001965 if (kIsDebugBuild) {
1966 switch (ret->InputAt(0)->GetType()) {
1967 case Primitive::kPrimBoolean:
1968 case Primitive::kPrimByte:
1969 case Primitive::kPrimChar:
1970 case Primitive::kPrimShort:
1971 case Primitive::kPrimInt:
1972 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001973 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001974 break;
1975
1976 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001977 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1978 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001979 break;
1980
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001981 case Primitive::kPrimFloat:
1982 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001983 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001984 break;
1985
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001986 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001987 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001988 }
1989 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001990 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001991}
1992
Calin Juravle175dc732015-08-25 15:42:32 +01001993void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1994 // The trampoline uses the same calling convention as dex calling conventions,
1995 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1996 // the method_idx.
1997 HandleInvoke(invoke);
1998}
1999
2000void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2001 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2002}
2003
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002004void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002005 // Explicit clinit checks triggered by static invokes must have been pruned by
2006 // art::PrepareForRegisterAllocation.
2007 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002008
Mark Mendellfb8d2792015-03-31 22:16:59 -04002009 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002010 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002011 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002012 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002013 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002014 return;
2015 }
2016
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002017 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002018
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002019 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2020 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002021 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002022 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002023}
2024
Mark Mendell09ed1a32015-03-25 08:30:06 -04002025static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2026 if (invoke->GetLocations()->Intrinsified()) {
2027 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2028 intrinsic.Dispatch(invoke);
2029 return true;
2030 }
2031 return false;
2032}
2033
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002034void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002035 // Explicit clinit checks triggered by static invokes must have been pruned by
2036 // art::PrepareForRegisterAllocation.
2037 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002038
Mark Mendell09ed1a32015-03-25 08:30:06 -04002039 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2040 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002041 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002042
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002043 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002044 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002045 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002046 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002047}
2048
2049void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002050 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2051 if (intrinsic.TryDispatch(invoke)) {
2052 return;
2053 }
2054
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002055 HandleInvoke(invoke);
2056}
2057
2058void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002059 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002060 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002061}
2062
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002063void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002064 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2065 return;
2066 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002067
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002068 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002069 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002070 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002071}
2072
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002073void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002074 // This call to HandleInvoke allocates a temporary (core) register
2075 // which is also used to transfer the hidden argument from FP to
2076 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002077 HandleInvoke(invoke);
2078 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002079 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002080}
2081
2082void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2083 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002084 LocationSummary* locations = invoke->GetLocations();
2085 Register temp = locations->GetTemp(0).AsRegister<Register>();
2086 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002087 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2088 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002089 Location receiver = locations->InAt(0);
2090 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2091
Roland Levillain0d5a2812015-11-13 10:07:31 +00002092 // Set the hidden argument. This is safe to do this here, as XMM7
2093 // won't be modified thereafter, before the `call` instruction.
2094 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002095 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002096 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002097
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002098 if (receiver.IsStackSlot()) {
2099 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002100 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002101 __ movl(temp, Address(temp, class_offset));
2102 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002103 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002104 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002105 }
Roland Levillain4d027112015-07-01 15:41:14 +01002106 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002107 // Instead of simply (possibly) unpoisoning `temp` here, we should
2108 // emit a read barrier for the previous class reference load.
2109 // However this is not required in practice, as this is an
2110 // intermediate/temporary reference and because the current
2111 // concurrent copying collector keeps the from-space memory
2112 // intact/accessible until the end of the marking phase (the
2113 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002114 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002115 // temp = temp->GetImtEntryAt(method_offset);
2116 __ movl(temp, Address(temp, method_offset));
2117 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002118 __ call(Address(temp,
2119 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002120
2121 DCHECK(!codegen_->IsLeafMethod());
2122 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2123}
2124
Roland Levillain88cb1752014-10-20 16:36:47 +01002125void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2126 LocationSummary* locations =
2127 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2128 switch (neg->GetResultType()) {
2129 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002130 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002131 locations->SetInAt(0, Location::RequiresRegister());
2132 locations->SetOut(Location::SameAsFirstInput());
2133 break;
2134
Roland Levillain88cb1752014-10-20 16:36:47 +01002135 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002136 locations->SetInAt(0, Location::RequiresFpuRegister());
2137 locations->SetOut(Location::SameAsFirstInput());
2138 locations->AddTemp(Location::RequiresRegister());
2139 locations->AddTemp(Location::RequiresFpuRegister());
2140 break;
2141
Roland Levillain88cb1752014-10-20 16:36:47 +01002142 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002143 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002144 locations->SetOut(Location::SameAsFirstInput());
2145 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002146 break;
2147
2148 default:
2149 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2150 }
2151}
2152
2153void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2154 LocationSummary* locations = neg->GetLocations();
2155 Location out = locations->Out();
2156 Location in = locations->InAt(0);
2157 switch (neg->GetResultType()) {
2158 case Primitive::kPrimInt:
2159 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002160 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002161 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002162 break;
2163
2164 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002165 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002166 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002167 __ negl(out.AsRegisterPairLow<Register>());
2168 // Negation is similar to subtraction from zero. The least
2169 // significant byte triggers a borrow when it is different from
2170 // zero; to take it into account, add 1 to the most significant
2171 // byte if the carry flag (CF) is set to 1 after the first NEGL
2172 // operation.
2173 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2174 __ negl(out.AsRegisterPairHigh<Register>());
2175 break;
2176
Roland Levillain5368c212014-11-27 15:03:41 +00002177 case Primitive::kPrimFloat: {
2178 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002179 Register constant = locations->GetTemp(0).AsRegister<Register>();
2180 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002181 // Implement float negation with an exclusive or with value
2182 // 0x80000000 (mask for bit 31, representing the sign of a
2183 // single-precision floating-point number).
2184 __ movl(constant, Immediate(INT32_C(0x80000000)));
2185 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002186 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002187 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002188 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002189
Roland Levillain5368c212014-11-27 15:03:41 +00002190 case Primitive::kPrimDouble: {
2191 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002192 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002193 // Implement double negation with an exclusive or with value
2194 // 0x8000000000000000 (mask for bit 63, representing the sign of
2195 // a double-precision floating-point number).
2196 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002197 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002198 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002199 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002200
2201 default:
2202 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2203 }
2204}
2205
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002206void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2207 LocationSummary* locations =
2208 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2209 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2210 locations->SetInAt(0, Location::RequiresFpuRegister());
2211 locations->SetInAt(1, Location::RequiresRegister());
2212 locations->SetOut(Location::SameAsFirstInput());
2213 locations->AddTemp(Location::RequiresFpuRegister());
2214}
2215
2216void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2217 LocationSummary* locations = neg->GetLocations();
2218 Location out = locations->Out();
2219 DCHECK(locations->InAt(0).Equals(out));
2220
2221 Register constant_area = locations->InAt(1).AsRegister<Register>();
2222 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2223 if (neg->GetType() == Primitive::kPrimFloat) {
2224 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2225 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2226 } else {
2227 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2228 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2229 }
2230}
2231
Roland Levillaindff1f282014-11-05 14:15:05 +00002232void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002233 Primitive::Type result_type = conversion->GetResultType();
2234 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002235 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002236
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002237 // The float-to-long and double-to-long type conversions rely on a
2238 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002239 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002240 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2241 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002242 ? LocationSummary::kCall
2243 : LocationSummary::kNoCall;
2244 LocationSummary* locations =
2245 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2246
David Brazdilb2bd1c52015-03-25 11:17:37 +00002247 // The Java language does not allow treating boolean as an integral type but
2248 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002249
Roland Levillaindff1f282014-11-05 14:15:05 +00002250 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002251 case Primitive::kPrimByte:
2252 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002253 case Primitive::kPrimLong: {
2254 // Type conversion from long to byte is a result of code transformations.
2255 HInstruction* input = conversion->InputAt(0);
2256 Location input_location = input->IsConstant()
2257 ? Location::ConstantLocation(input->AsConstant())
2258 : Location::RegisterPairLocation(EAX, EDX);
2259 locations->SetInAt(0, input_location);
2260 // Make the output overlap to please the register allocator. This greatly simplifies
2261 // the validation of the linear scan implementation
2262 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2263 break;
2264 }
David Brazdil46e2a392015-03-16 17:31:52 +00002265 case Primitive::kPrimBoolean:
2266 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002267 case Primitive::kPrimShort:
2268 case Primitive::kPrimInt:
2269 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002270 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002271 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2272 // Make the output overlap to please the register allocator. This greatly simplifies
2273 // the validation of the linear scan implementation
2274 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002275 break;
2276
2277 default:
2278 LOG(FATAL) << "Unexpected type conversion from " << input_type
2279 << " to " << result_type;
2280 }
2281 break;
2282
Roland Levillain01a8d712014-11-14 16:27:39 +00002283 case Primitive::kPrimShort:
2284 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002285 case Primitive::kPrimLong:
2286 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002287 case Primitive::kPrimBoolean:
2288 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002289 case Primitive::kPrimByte:
2290 case Primitive::kPrimInt:
2291 case Primitive::kPrimChar:
2292 // Processing a Dex `int-to-short' instruction.
2293 locations->SetInAt(0, Location::Any());
2294 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2295 break;
2296
2297 default:
2298 LOG(FATAL) << "Unexpected type conversion from " << input_type
2299 << " to " << result_type;
2300 }
2301 break;
2302
Roland Levillain946e1432014-11-11 17:35:19 +00002303 case Primitive::kPrimInt:
2304 switch (input_type) {
2305 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002306 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002307 locations->SetInAt(0, Location::Any());
2308 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2309 break;
2310
2311 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002312 // Processing a Dex `float-to-int' instruction.
2313 locations->SetInAt(0, Location::RequiresFpuRegister());
2314 locations->SetOut(Location::RequiresRegister());
2315 locations->AddTemp(Location::RequiresFpuRegister());
2316 break;
2317
Roland Levillain946e1432014-11-11 17:35:19 +00002318 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002319 // Processing a Dex `double-to-int' instruction.
2320 locations->SetInAt(0, Location::RequiresFpuRegister());
2321 locations->SetOut(Location::RequiresRegister());
2322 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002323 break;
2324
2325 default:
2326 LOG(FATAL) << "Unexpected type conversion from " << input_type
2327 << " to " << result_type;
2328 }
2329 break;
2330
Roland Levillaindff1f282014-11-05 14:15:05 +00002331 case Primitive::kPrimLong:
2332 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002333 case Primitive::kPrimBoolean:
2334 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002335 case Primitive::kPrimByte:
2336 case Primitive::kPrimShort:
2337 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002338 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002339 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002340 locations->SetInAt(0, Location::RegisterLocation(EAX));
2341 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2342 break;
2343
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002344 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002345 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002346 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002347 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002348 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2349 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2350
Vladimir Marko949c91f2015-01-27 10:48:44 +00002351 // The runtime helper puts the result in EAX, EDX.
2352 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002353 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002354 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002355
2356 default:
2357 LOG(FATAL) << "Unexpected type conversion from " << input_type
2358 << " to " << result_type;
2359 }
2360 break;
2361
Roland Levillain981e4542014-11-14 11:47:14 +00002362 case Primitive::kPrimChar:
2363 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002364 case Primitive::kPrimLong:
2365 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002366 case Primitive::kPrimBoolean:
2367 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002368 case Primitive::kPrimByte:
2369 case Primitive::kPrimShort:
2370 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002371 // Processing a Dex `int-to-char' instruction.
2372 locations->SetInAt(0, Location::Any());
2373 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2374 break;
2375
2376 default:
2377 LOG(FATAL) << "Unexpected type conversion from " << input_type
2378 << " to " << result_type;
2379 }
2380 break;
2381
Roland Levillaindff1f282014-11-05 14:15:05 +00002382 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002383 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002384 case Primitive::kPrimBoolean:
2385 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002386 case Primitive::kPrimByte:
2387 case Primitive::kPrimShort:
2388 case Primitive::kPrimInt:
2389 case Primitive::kPrimChar:
2390 // Processing a Dex `int-to-float' instruction.
2391 locations->SetInAt(0, Location::RequiresRegister());
2392 locations->SetOut(Location::RequiresFpuRegister());
2393 break;
2394
2395 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002396 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002397 locations->SetInAt(0, Location::Any());
2398 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002399 break;
2400
Roland Levillaincff13742014-11-17 14:32:17 +00002401 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002402 // Processing a Dex `double-to-float' instruction.
2403 locations->SetInAt(0, Location::RequiresFpuRegister());
2404 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002405 break;
2406
2407 default:
2408 LOG(FATAL) << "Unexpected type conversion from " << input_type
2409 << " to " << result_type;
2410 };
2411 break;
2412
Roland Levillaindff1f282014-11-05 14:15:05 +00002413 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002414 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002415 case Primitive::kPrimBoolean:
2416 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002417 case Primitive::kPrimByte:
2418 case Primitive::kPrimShort:
2419 case Primitive::kPrimInt:
2420 case Primitive::kPrimChar:
2421 // Processing a Dex `int-to-double' instruction.
2422 locations->SetInAt(0, Location::RequiresRegister());
2423 locations->SetOut(Location::RequiresFpuRegister());
2424 break;
2425
2426 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002427 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002428 locations->SetInAt(0, Location::Any());
2429 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002430 break;
2431
Roland Levillaincff13742014-11-17 14:32:17 +00002432 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002433 // Processing a Dex `float-to-double' instruction.
2434 locations->SetInAt(0, Location::RequiresFpuRegister());
2435 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002436 break;
2437
2438 default:
2439 LOG(FATAL) << "Unexpected type conversion from " << input_type
2440 << " to " << result_type;
2441 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002442 break;
2443
2444 default:
2445 LOG(FATAL) << "Unexpected type conversion from " << input_type
2446 << " to " << result_type;
2447 }
2448}
2449
2450void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2451 LocationSummary* locations = conversion->GetLocations();
2452 Location out = locations->Out();
2453 Location in = locations->InAt(0);
2454 Primitive::Type result_type = conversion->GetResultType();
2455 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002456 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002457 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002458 case Primitive::kPrimByte:
2459 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002460 case Primitive::kPrimLong:
2461 // Type conversion from long to byte is a result of code transformations.
2462 if (in.IsRegisterPair()) {
2463 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2464 } else {
2465 DCHECK(in.GetConstant()->IsLongConstant());
2466 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2467 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2468 }
2469 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002470 case Primitive::kPrimBoolean:
2471 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002472 case Primitive::kPrimShort:
2473 case Primitive::kPrimInt:
2474 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002475 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002476 if (in.IsRegister()) {
2477 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002478 } else {
2479 DCHECK(in.GetConstant()->IsIntConstant());
2480 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2481 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2482 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002483 break;
2484
2485 default:
2486 LOG(FATAL) << "Unexpected type conversion from " << input_type
2487 << " to " << result_type;
2488 }
2489 break;
2490
Roland Levillain01a8d712014-11-14 16:27:39 +00002491 case Primitive::kPrimShort:
2492 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002493 case Primitive::kPrimLong:
2494 // Type conversion from long to short is a result of code transformations.
2495 if (in.IsRegisterPair()) {
2496 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2497 } else if (in.IsDoubleStackSlot()) {
2498 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2499 } else {
2500 DCHECK(in.GetConstant()->IsLongConstant());
2501 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2502 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2503 }
2504 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002505 case Primitive::kPrimBoolean:
2506 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002507 case Primitive::kPrimByte:
2508 case Primitive::kPrimInt:
2509 case Primitive::kPrimChar:
2510 // Processing a Dex `int-to-short' instruction.
2511 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002512 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002513 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002514 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002515 } else {
2516 DCHECK(in.GetConstant()->IsIntConstant());
2517 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002518 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002519 }
2520 break;
2521
2522 default:
2523 LOG(FATAL) << "Unexpected type conversion from " << input_type
2524 << " to " << result_type;
2525 }
2526 break;
2527
Roland Levillain946e1432014-11-11 17:35:19 +00002528 case Primitive::kPrimInt:
2529 switch (input_type) {
2530 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002531 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002532 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002533 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002534 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002535 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002536 } else {
2537 DCHECK(in.IsConstant());
2538 DCHECK(in.GetConstant()->IsLongConstant());
2539 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002540 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002541 }
2542 break;
2543
Roland Levillain3f8f9362014-12-02 17:45:01 +00002544 case Primitive::kPrimFloat: {
2545 // Processing a Dex `float-to-int' instruction.
2546 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2547 Register output = out.AsRegister<Register>();
2548 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002549 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002550
2551 __ movl(output, Immediate(kPrimIntMax));
2552 // temp = int-to-float(output)
2553 __ cvtsi2ss(temp, output);
2554 // if input >= temp goto done
2555 __ comiss(input, temp);
2556 __ j(kAboveEqual, &done);
2557 // if input == NaN goto nan
2558 __ j(kUnordered, &nan);
2559 // output = float-to-int-truncate(input)
2560 __ cvttss2si(output, input);
2561 __ jmp(&done);
2562 __ Bind(&nan);
2563 // output = 0
2564 __ xorl(output, output);
2565 __ Bind(&done);
2566 break;
2567 }
2568
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002569 case Primitive::kPrimDouble: {
2570 // Processing a Dex `double-to-int' instruction.
2571 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2572 Register output = out.AsRegister<Register>();
2573 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002574 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002575
2576 __ movl(output, Immediate(kPrimIntMax));
2577 // temp = int-to-double(output)
2578 __ cvtsi2sd(temp, output);
2579 // if input >= temp goto done
2580 __ comisd(input, temp);
2581 __ j(kAboveEqual, &done);
2582 // if input == NaN goto nan
2583 __ j(kUnordered, &nan);
2584 // output = double-to-int-truncate(input)
2585 __ cvttsd2si(output, input);
2586 __ jmp(&done);
2587 __ Bind(&nan);
2588 // output = 0
2589 __ xorl(output, output);
2590 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002591 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002592 }
Roland Levillain946e1432014-11-11 17:35:19 +00002593
2594 default:
2595 LOG(FATAL) << "Unexpected type conversion from " << input_type
2596 << " to " << result_type;
2597 }
2598 break;
2599
Roland Levillaindff1f282014-11-05 14:15:05 +00002600 case Primitive::kPrimLong:
2601 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002602 case Primitive::kPrimBoolean:
2603 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002604 case Primitive::kPrimByte:
2605 case Primitive::kPrimShort:
2606 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002607 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002608 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002609 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2610 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002611 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002612 __ cdq();
2613 break;
2614
2615 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002616 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002617 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2618 conversion,
2619 conversion->GetDexPc(),
2620 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002621 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002622 break;
2623
Roland Levillaindff1f282014-11-05 14:15:05 +00002624 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002625 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002626 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2627 conversion,
2628 conversion->GetDexPc(),
2629 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002630 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002631 break;
2632
2633 default:
2634 LOG(FATAL) << "Unexpected type conversion from " << input_type
2635 << " to " << result_type;
2636 }
2637 break;
2638
Roland Levillain981e4542014-11-14 11:47:14 +00002639 case Primitive::kPrimChar:
2640 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002641 case Primitive::kPrimLong:
2642 // Type conversion from long to short is a result of code transformations.
2643 if (in.IsRegisterPair()) {
2644 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2645 } else if (in.IsDoubleStackSlot()) {
2646 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2647 } else {
2648 DCHECK(in.GetConstant()->IsLongConstant());
2649 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2650 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2651 }
2652 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002653 case Primitive::kPrimBoolean:
2654 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002655 case Primitive::kPrimByte:
2656 case Primitive::kPrimShort:
2657 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002658 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2659 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002660 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002661 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002662 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002663 } else {
2664 DCHECK(in.GetConstant()->IsIntConstant());
2665 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002666 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002667 }
2668 break;
2669
2670 default:
2671 LOG(FATAL) << "Unexpected type conversion from " << input_type
2672 << " to " << result_type;
2673 }
2674 break;
2675
Roland Levillaindff1f282014-11-05 14:15:05 +00002676 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002677 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002678 case Primitive::kPrimBoolean:
2679 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002680 case Primitive::kPrimByte:
2681 case Primitive::kPrimShort:
2682 case Primitive::kPrimInt:
2683 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002684 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002685 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002686 break;
2687
Roland Levillain6d0e4832014-11-27 18:31:21 +00002688 case Primitive::kPrimLong: {
2689 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002690 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002691
Roland Levillain232ade02015-04-20 15:14:36 +01002692 // Create stack space for the call to
2693 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2694 // TODO: enhance register allocator to ask for stack temporaries.
2695 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2696 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2697 __ subl(ESP, Immediate(adjustment));
2698 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002699
Roland Levillain232ade02015-04-20 15:14:36 +01002700 // Load the value to the FP stack, using temporaries if needed.
2701 PushOntoFPStack(in, 0, adjustment, false, true);
2702
2703 if (out.IsStackSlot()) {
2704 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2705 } else {
2706 __ fstps(Address(ESP, 0));
2707 Location stack_temp = Location::StackSlot(0);
2708 codegen_->Move32(out, stack_temp);
2709 }
2710
2711 // Remove the temporary stack space we allocated.
2712 if (adjustment != 0) {
2713 __ addl(ESP, Immediate(adjustment));
2714 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002715 break;
2716 }
2717
Roland Levillaincff13742014-11-17 14:32:17 +00002718 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002719 // Processing a Dex `double-to-float' instruction.
2720 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002721 break;
2722
2723 default:
2724 LOG(FATAL) << "Unexpected type conversion from " << input_type
2725 << " to " << result_type;
2726 };
2727 break;
2728
Roland Levillaindff1f282014-11-05 14:15:05 +00002729 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002730 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002731 case Primitive::kPrimBoolean:
2732 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002733 case Primitive::kPrimByte:
2734 case Primitive::kPrimShort:
2735 case Primitive::kPrimInt:
2736 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002737 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002738 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002739 break;
2740
Roland Levillain647b9ed2014-11-27 12:06:00 +00002741 case Primitive::kPrimLong: {
2742 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002743 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002744
Roland Levillain232ade02015-04-20 15:14:36 +01002745 // Create stack space for the call to
2746 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2747 // TODO: enhance register allocator to ask for stack temporaries.
2748 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2749 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2750 __ subl(ESP, Immediate(adjustment));
2751 }
2752
2753 // Load the value to the FP stack, using temporaries if needed.
2754 PushOntoFPStack(in, 0, adjustment, false, true);
2755
2756 if (out.IsDoubleStackSlot()) {
2757 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2758 } else {
2759 __ fstpl(Address(ESP, 0));
2760 Location stack_temp = Location::DoubleStackSlot(0);
2761 codegen_->Move64(out, stack_temp);
2762 }
2763
2764 // Remove the temporary stack space we allocated.
2765 if (adjustment != 0) {
2766 __ addl(ESP, Immediate(adjustment));
2767 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002768 break;
2769 }
2770
Roland Levillaincff13742014-11-17 14:32:17 +00002771 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002772 // Processing a Dex `float-to-double' instruction.
2773 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002774 break;
2775
2776 default:
2777 LOG(FATAL) << "Unexpected type conversion from " << input_type
2778 << " to " << result_type;
2779 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002780 break;
2781
2782 default:
2783 LOG(FATAL) << "Unexpected type conversion from " << input_type
2784 << " to " << result_type;
2785 }
2786}
2787
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002788void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002789 LocationSummary* locations =
2790 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002791 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002792 case Primitive::kPrimInt: {
2793 locations->SetInAt(0, Location::RequiresRegister());
2794 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2795 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2796 break;
2797 }
2798
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002799 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002800 locations->SetInAt(0, Location::RequiresRegister());
2801 locations->SetInAt(1, Location::Any());
2802 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002803 break;
2804 }
2805
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002806 case Primitive::kPrimFloat:
2807 case Primitive::kPrimDouble: {
2808 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002809 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2810 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002811 } else if (add->InputAt(1)->IsConstant()) {
2812 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002813 } else {
2814 locations->SetInAt(1, Location::Any());
2815 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002816 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002817 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002818 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002819
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002820 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002821 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2822 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002823 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002824}
2825
2826void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2827 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002828 Location first = locations->InAt(0);
2829 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002830 Location out = locations->Out();
2831
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002832 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002833 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002834 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002835 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2836 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002837 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2838 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002839 } else {
2840 __ leal(out.AsRegister<Register>(), Address(
2841 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2842 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002843 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002844 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2845 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2846 __ addl(out.AsRegister<Register>(), Immediate(value));
2847 } else {
2848 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2849 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002850 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002851 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002852 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002853 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002854 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002855 }
2856
2857 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002858 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002859 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2860 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002861 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002862 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2863 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002864 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002865 } else {
2866 DCHECK(second.IsConstant()) << second;
2867 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2868 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2869 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002870 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002871 break;
2872 }
2873
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002874 case Primitive::kPrimFloat: {
2875 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002876 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002877 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2878 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002879 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002880 __ addss(first.AsFpuRegister<XmmRegister>(),
2881 codegen_->LiteralFloatAddress(
2882 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2883 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2884 } else {
2885 DCHECK(second.IsStackSlot());
2886 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002887 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002888 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002889 }
2890
2891 case Primitive::kPrimDouble: {
2892 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002893 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002894 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2895 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002896 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002897 __ addsd(first.AsFpuRegister<XmmRegister>(),
2898 codegen_->LiteralDoubleAddress(
2899 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2900 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2901 } else {
2902 DCHECK(second.IsDoubleStackSlot());
2903 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002904 }
2905 break;
2906 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002907
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002908 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002909 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002910 }
2911}
2912
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002913void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002914 LocationSummary* locations =
2915 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002916 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002917 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002918 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002919 locations->SetInAt(0, Location::RequiresRegister());
2920 locations->SetInAt(1, Location::Any());
2921 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002922 break;
2923 }
Calin Juravle11351682014-10-23 15:38:15 +01002924 case Primitive::kPrimFloat:
2925 case Primitive::kPrimDouble: {
2926 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002927 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2928 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002929 } else if (sub->InputAt(1)->IsConstant()) {
2930 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002931 } else {
2932 locations->SetInAt(1, Location::Any());
2933 }
Calin Juravle11351682014-10-23 15:38:15 +01002934 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002935 break;
Calin Juravle11351682014-10-23 15:38:15 +01002936 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002937
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002938 default:
Calin Juravle11351682014-10-23 15:38:15 +01002939 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002940 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002941}
2942
2943void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2944 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002945 Location first = locations->InAt(0);
2946 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002947 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002948 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002949 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002950 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002951 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002952 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002953 __ subl(first.AsRegister<Register>(),
2954 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002955 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002956 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002957 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002958 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002959 }
2960
2961 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002962 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002963 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2964 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002965 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002966 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002967 __ sbbl(first.AsRegisterPairHigh<Register>(),
2968 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002969 } else {
2970 DCHECK(second.IsConstant()) << second;
2971 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2972 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2973 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002974 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002975 break;
2976 }
2977
Calin Juravle11351682014-10-23 15:38:15 +01002978 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002979 if (second.IsFpuRegister()) {
2980 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2981 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2982 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002983 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002984 __ subss(first.AsFpuRegister<XmmRegister>(),
2985 codegen_->LiteralFloatAddress(
2986 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2987 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2988 } else {
2989 DCHECK(second.IsStackSlot());
2990 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2991 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002992 break;
Calin Juravle11351682014-10-23 15:38:15 +01002993 }
2994
2995 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002996 if (second.IsFpuRegister()) {
2997 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2998 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2999 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003000 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003001 __ subsd(first.AsFpuRegister<XmmRegister>(),
3002 codegen_->LiteralDoubleAddress(
3003 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3004 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3005 } else {
3006 DCHECK(second.IsDoubleStackSlot());
3007 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3008 }
Calin Juravle11351682014-10-23 15:38:15 +01003009 break;
3010 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003011
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003012 default:
Calin Juravle11351682014-10-23 15:38:15 +01003013 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003014 }
3015}
3016
Calin Juravle34bacdf2014-10-07 20:23:36 +01003017void LocationsBuilderX86::VisitMul(HMul* mul) {
3018 LocationSummary* locations =
3019 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3020 switch (mul->GetResultType()) {
3021 case Primitive::kPrimInt:
3022 locations->SetInAt(0, Location::RequiresRegister());
3023 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003024 if (mul->InputAt(1)->IsIntConstant()) {
3025 // Can use 3 operand multiply.
3026 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3027 } else {
3028 locations->SetOut(Location::SameAsFirstInput());
3029 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003030 break;
3031 case Primitive::kPrimLong: {
3032 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003033 locations->SetInAt(1, Location::Any());
3034 locations->SetOut(Location::SameAsFirstInput());
3035 // Needed for imul on 32bits with 64bits output.
3036 locations->AddTemp(Location::RegisterLocation(EAX));
3037 locations->AddTemp(Location::RegisterLocation(EDX));
3038 break;
3039 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003040 case Primitive::kPrimFloat:
3041 case Primitive::kPrimDouble: {
3042 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003043 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3044 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003045 } else if (mul->InputAt(1)->IsConstant()) {
3046 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003047 } else {
3048 locations->SetInAt(1, Location::Any());
3049 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003050 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003051 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003052 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003053
3054 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003055 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003056 }
3057}
3058
3059void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3060 LocationSummary* locations = mul->GetLocations();
3061 Location first = locations->InAt(0);
3062 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003063 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003064
3065 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003066 case Primitive::kPrimInt:
3067 // The constant may have ended up in a register, so test explicitly to avoid
3068 // problems where the output may not be the same as the first operand.
3069 if (mul->InputAt(1)->IsIntConstant()) {
3070 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3071 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3072 } else if (second.IsRegister()) {
3073 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003074 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003075 } else {
3076 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003077 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003078 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003079 }
3080 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003081
3082 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003083 Register in1_hi = first.AsRegisterPairHigh<Register>();
3084 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003085 Register eax = locations->GetTemp(0).AsRegister<Register>();
3086 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003087
3088 DCHECK_EQ(EAX, eax);
3089 DCHECK_EQ(EDX, edx);
3090
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003091 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003092 // output: in1
3093 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3094 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3095 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003096 if (second.IsConstant()) {
3097 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003098
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003099 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3100 int32_t low_value = Low32Bits(value);
3101 int32_t high_value = High32Bits(value);
3102 Immediate low(low_value);
3103 Immediate high(high_value);
3104
3105 __ movl(eax, high);
3106 // eax <- in1.lo * in2.hi
3107 __ imull(eax, in1_lo);
3108 // in1.hi <- in1.hi * in2.lo
3109 __ imull(in1_hi, low);
3110 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3111 __ addl(in1_hi, eax);
3112 // move in2_lo to eax to prepare for double precision
3113 __ movl(eax, low);
3114 // edx:eax <- in1.lo * in2.lo
3115 __ mull(in1_lo);
3116 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3117 __ addl(in1_hi, edx);
3118 // in1.lo <- (in1.lo * in2.lo)[31:0];
3119 __ movl(in1_lo, eax);
3120 } else if (second.IsRegisterPair()) {
3121 Register in2_hi = second.AsRegisterPairHigh<Register>();
3122 Register in2_lo = second.AsRegisterPairLow<Register>();
3123
3124 __ movl(eax, in2_hi);
3125 // eax <- in1.lo * in2.hi
3126 __ imull(eax, in1_lo);
3127 // in1.hi <- in1.hi * in2.lo
3128 __ imull(in1_hi, in2_lo);
3129 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3130 __ addl(in1_hi, eax);
3131 // move in1_lo to eax to prepare for double precision
3132 __ movl(eax, in1_lo);
3133 // edx:eax <- in1.lo * in2.lo
3134 __ mull(in2_lo);
3135 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3136 __ addl(in1_hi, edx);
3137 // in1.lo <- (in1.lo * in2.lo)[31:0];
3138 __ movl(in1_lo, eax);
3139 } else {
3140 DCHECK(second.IsDoubleStackSlot()) << second;
3141 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3142 Address in2_lo(ESP, second.GetStackIndex());
3143
3144 __ movl(eax, in2_hi);
3145 // eax <- in1.lo * in2.hi
3146 __ imull(eax, in1_lo);
3147 // in1.hi <- in1.hi * in2.lo
3148 __ imull(in1_hi, in2_lo);
3149 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3150 __ addl(in1_hi, eax);
3151 // move in1_lo to eax to prepare for double precision
3152 __ movl(eax, in1_lo);
3153 // edx:eax <- in1.lo * in2.lo
3154 __ mull(in2_lo);
3155 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3156 __ addl(in1_hi, edx);
3157 // in1.lo <- (in1.lo * in2.lo)[31:0];
3158 __ movl(in1_lo, eax);
3159 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003160
3161 break;
3162 }
3163
Calin Juravleb5bfa962014-10-21 18:02:24 +01003164 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003165 DCHECK(first.Equals(locations->Out()));
3166 if (second.IsFpuRegister()) {
3167 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3168 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3169 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003170 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003171 __ mulss(first.AsFpuRegister<XmmRegister>(),
3172 codegen_->LiteralFloatAddress(
3173 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3174 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3175 } else {
3176 DCHECK(second.IsStackSlot());
3177 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3178 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003179 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003180 }
3181
3182 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003183 DCHECK(first.Equals(locations->Out()));
3184 if (second.IsFpuRegister()) {
3185 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3186 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3187 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003188 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003189 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3190 codegen_->LiteralDoubleAddress(
3191 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3192 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3193 } else {
3194 DCHECK(second.IsDoubleStackSlot());
3195 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3196 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003197 break;
3198 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003199
3200 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003201 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003202 }
3203}
3204
Roland Levillain232ade02015-04-20 15:14:36 +01003205void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3206 uint32_t temp_offset,
3207 uint32_t stack_adjustment,
3208 bool is_fp,
3209 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003210 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003211 DCHECK(!is_wide);
3212 if (is_fp) {
3213 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3214 } else {
3215 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3216 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003217 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003218 DCHECK(is_wide);
3219 if (is_fp) {
3220 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3221 } else {
3222 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3223 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003224 } else {
3225 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003226 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003227 Location stack_temp = Location::StackSlot(temp_offset);
3228 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003229 if (is_fp) {
3230 __ flds(Address(ESP, temp_offset));
3231 } else {
3232 __ filds(Address(ESP, temp_offset));
3233 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003234 } else {
3235 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3236 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003237 if (is_fp) {
3238 __ fldl(Address(ESP, temp_offset));
3239 } else {
3240 __ fildl(Address(ESP, temp_offset));
3241 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003242 }
3243 }
3244}
3245
3246void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3247 Primitive::Type type = rem->GetResultType();
3248 bool is_float = type == Primitive::kPrimFloat;
3249 size_t elem_size = Primitive::ComponentSize(type);
3250 LocationSummary* locations = rem->GetLocations();
3251 Location first = locations->InAt(0);
3252 Location second = locations->InAt(1);
3253 Location out = locations->Out();
3254
3255 // Create stack space for 2 elements.
3256 // TODO: enhance register allocator to ask for stack temporaries.
3257 __ subl(ESP, Immediate(2 * elem_size));
3258
3259 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003260 const bool is_wide = !is_float;
3261 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3262 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003263
3264 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003265 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003266 __ Bind(&retry);
3267 __ fprem();
3268
3269 // Move FP status to AX.
3270 __ fstsw();
3271
3272 // And see if the argument reduction is complete. This is signaled by the
3273 // C2 FPU flag bit set to 0.
3274 __ andl(EAX, Immediate(kC2ConditionMask));
3275 __ j(kNotEqual, &retry);
3276
3277 // We have settled on the final value. Retrieve it into an XMM register.
3278 // Store FP top of stack to real stack.
3279 if (is_float) {
3280 __ fsts(Address(ESP, 0));
3281 } else {
3282 __ fstl(Address(ESP, 0));
3283 }
3284
3285 // Pop the 2 items from the FP stack.
3286 __ fucompp();
3287
3288 // Load the value from the stack into an XMM register.
3289 DCHECK(out.IsFpuRegister()) << out;
3290 if (is_float) {
3291 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3292 } else {
3293 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3294 }
3295
3296 // And remove the temporary stack space we allocated.
3297 __ addl(ESP, Immediate(2 * elem_size));
3298}
3299
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003300
3301void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3302 DCHECK(instruction->IsDiv() || instruction->IsRem());
3303
3304 LocationSummary* locations = instruction->GetLocations();
3305 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003306 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003307
3308 Register out_register = locations->Out().AsRegister<Register>();
3309 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003310 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003311
3312 DCHECK(imm == 1 || imm == -1);
3313
3314 if (instruction->IsRem()) {
3315 __ xorl(out_register, out_register);
3316 } else {
3317 __ movl(out_register, input_register);
3318 if (imm == -1) {
3319 __ negl(out_register);
3320 }
3321 }
3322}
3323
3324
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003325void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003326 LocationSummary* locations = instruction->GetLocations();
3327
3328 Register out_register = locations->Out().AsRegister<Register>();
3329 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003330 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003331 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3332 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003333
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003334 Register num = locations->GetTemp(0).AsRegister<Register>();
3335
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003336 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003337 __ testl(input_register, input_register);
3338 __ cmovl(kGreaterEqual, num, input_register);
3339 int shift = CTZ(imm);
3340 __ sarl(num, Immediate(shift));
3341
3342 if (imm < 0) {
3343 __ negl(num);
3344 }
3345
3346 __ movl(out_register, num);
3347}
3348
3349void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3350 DCHECK(instruction->IsDiv() || instruction->IsRem());
3351
3352 LocationSummary* locations = instruction->GetLocations();
3353 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3354
3355 Register eax = locations->InAt(0).AsRegister<Register>();
3356 Register out = locations->Out().AsRegister<Register>();
3357 Register num;
3358 Register edx;
3359
3360 if (instruction->IsDiv()) {
3361 edx = locations->GetTemp(0).AsRegister<Register>();
3362 num = locations->GetTemp(1).AsRegister<Register>();
3363 } else {
3364 edx = locations->Out().AsRegister<Register>();
3365 num = locations->GetTemp(0).AsRegister<Register>();
3366 }
3367
3368 DCHECK_EQ(EAX, eax);
3369 DCHECK_EQ(EDX, edx);
3370 if (instruction->IsDiv()) {
3371 DCHECK_EQ(EAX, out);
3372 } else {
3373 DCHECK_EQ(EDX, out);
3374 }
3375
3376 int64_t magic;
3377 int shift;
3378 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3379
Mark Mendell0c9497d2015-08-21 09:30:05 -04003380 NearLabel ndiv;
3381 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003382 // If numerator is 0, the result is 0, no computation needed.
3383 __ testl(eax, eax);
3384 __ j(kNotEqual, &ndiv);
3385
3386 __ xorl(out, out);
3387 __ jmp(&end);
3388
3389 __ Bind(&ndiv);
3390
3391 // Save the numerator.
3392 __ movl(num, eax);
3393
3394 // EAX = magic
3395 __ movl(eax, Immediate(magic));
3396
3397 // EDX:EAX = magic * numerator
3398 __ imull(num);
3399
3400 if (imm > 0 && magic < 0) {
3401 // EDX += num
3402 __ addl(edx, num);
3403 } else if (imm < 0 && magic > 0) {
3404 __ subl(edx, num);
3405 }
3406
3407 // Shift if needed.
3408 if (shift != 0) {
3409 __ sarl(edx, Immediate(shift));
3410 }
3411
3412 // EDX += 1 if EDX < 0
3413 __ movl(eax, edx);
3414 __ shrl(edx, Immediate(31));
3415 __ addl(edx, eax);
3416
3417 if (instruction->IsRem()) {
3418 __ movl(eax, num);
3419 __ imull(edx, Immediate(imm));
3420 __ subl(eax, edx);
3421 __ movl(edx, eax);
3422 } else {
3423 __ movl(eax, edx);
3424 }
3425 __ Bind(&end);
3426}
3427
Calin Juravlebacfec32014-11-14 15:54:36 +00003428void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3429 DCHECK(instruction->IsDiv() || instruction->IsRem());
3430
3431 LocationSummary* locations = instruction->GetLocations();
3432 Location out = locations->Out();
3433 Location first = locations->InAt(0);
3434 Location second = locations->InAt(1);
3435 bool is_div = instruction->IsDiv();
3436
3437 switch (instruction->GetResultType()) {
3438 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003439 DCHECK_EQ(EAX, first.AsRegister<Register>());
3440 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003441
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003442 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003443 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003444
3445 if (imm == 0) {
3446 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3447 } else if (imm == 1 || imm == -1) {
3448 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003449 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003450 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003451 } else {
3452 DCHECK(imm <= -2 || imm >= 2);
3453 GenerateDivRemWithAnyConstant(instruction);
3454 }
3455 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003456 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003457 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003458 is_div);
3459 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003460
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003461 Register second_reg = second.AsRegister<Register>();
3462 // 0x80000000/-1 triggers an arithmetic exception!
3463 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3464 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003465
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003466 __ cmpl(second_reg, Immediate(-1));
3467 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003468
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003469 // edx:eax <- sign-extended of eax
3470 __ cdq();
3471 // eax = quotient, edx = remainder
3472 __ idivl(second_reg);
3473 __ Bind(slow_path->GetExitLabel());
3474 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003475 break;
3476 }
3477
3478 case Primitive::kPrimLong: {
3479 InvokeRuntimeCallingConvention calling_convention;
3480 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3481 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3482 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3483 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3484 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3485 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3486
3487 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003488 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3489 instruction,
3490 instruction->GetDexPc(),
3491 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003492 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003493 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003494 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3495 instruction,
3496 instruction->GetDexPc(),
3497 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003498 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003499 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003500 break;
3501 }
3502
3503 default:
3504 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3505 }
3506}
3507
Calin Juravle7c4954d2014-10-28 16:57:40 +00003508void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003509 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003510 ? LocationSummary::kCall
3511 : LocationSummary::kNoCall;
3512 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3513
Calin Juravle7c4954d2014-10-28 16:57:40 +00003514 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003515 case Primitive::kPrimInt: {
3516 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003517 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003518 locations->SetOut(Location::SameAsFirstInput());
3519 // Intel uses edx:eax as the dividend.
3520 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003521 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3522 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3523 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003524 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003525 locations->AddTemp(Location::RequiresRegister());
3526 }
Calin Juravled0d48522014-11-04 16:40:20 +00003527 break;
3528 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003529 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003530 InvokeRuntimeCallingConvention calling_convention;
3531 locations->SetInAt(0, Location::RegisterPairLocation(
3532 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3533 locations->SetInAt(1, Location::RegisterPairLocation(
3534 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3535 // Runtime helper puts the result in EAX, EDX.
3536 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003537 break;
3538 }
3539 case Primitive::kPrimFloat:
3540 case Primitive::kPrimDouble: {
3541 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003542 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3543 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003544 } else if (div->InputAt(1)->IsConstant()) {
3545 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003546 } else {
3547 locations->SetInAt(1, Location::Any());
3548 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003549 locations->SetOut(Location::SameAsFirstInput());
3550 break;
3551 }
3552
3553 default:
3554 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3555 }
3556}
3557
3558void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3559 LocationSummary* locations = div->GetLocations();
3560 Location first = locations->InAt(0);
3561 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003562
3563 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003564 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003565 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003566 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003567 break;
3568 }
3569
3570 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003571 if (second.IsFpuRegister()) {
3572 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3573 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3574 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003575 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003576 __ divss(first.AsFpuRegister<XmmRegister>(),
3577 codegen_->LiteralFloatAddress(
3578 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3579 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3580 } else {
3581 DCHECK(second.IsStackSlot());
3582 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3583 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003584 break;
3585 }
3586
3587 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003588 if (second.IsFpuRegister()) {
3589 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3590 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3591 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003592 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003593 __ divsd(first.AsFpuRegister<XmmRegister>(),
3594 codegen_->LiteralDoubleAddress(
3595 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3596 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3597 } else {
3598 DCHECK(second.IsDoubleStackSlot());
3599 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3600 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003601 break;
3602 }
3603
3604 default:
3605 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3606 }
3607}
3608
Calin Juravlebacfec32014-11-14 15:54:36 +00003609void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003610 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003611
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003612 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3613 ? LocationSummary::kCall
3614 : LocationSummary::kNoCall;
3615 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003616
Calin Juravled2ec87d2014-12-08 14:24:46 +00003617 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003618 case Primitive::kPrimInt: {
3619 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003620 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003621 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003622 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3623 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3624 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003625 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003626 locations->AddTemp(Location::RequiresRegister());
3627 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003628 break;
3629 }
3630 case Primitive::kPrimLong: {
3631 InvokeRuntimeCallingConvention calling_convention;
3632 locations->SetInAt(0, Location::RegisterPairLocation(
3633 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3634 locations->SetInAt(1, Location::RegisterPairLocation(
3635 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3636 // Runtime helper puts the result in EAX, EDX.
3637 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3638 break;
3639 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003640 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003641 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003642 locations->SetInAt(0, Location::Any());
3643 locations->SetInAt(1, Location::Any());
3644 locations->SetOut(Location::RequiresFpuRegister());
3645 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003646 break;
3647 }
3648
3649 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003650 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003651 }
3652}
3653
3654void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3655 Primitive::Type type = rem->GetResultType();
3656 switch (type) {
3657 case Primitive::kPrimInt:
3658 case Primitive::kPrimLong: {
3659 GenerateDivRemIntegral(rem);
3660 break;
3661 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003662 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003663 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003664 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003665 break;
3666 }
3667 default:
3668 LOG(FATAL) << "Unexpected rem type " << type;
3669 }
3670}
3671
Calin Juravled0d48522014-11-04 16:40:20 +00003672void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003673 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3674 ? LocationSummary::kCallOnSlowPath
3675 : LocationSummary::kNoCall;
3676 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003677 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003678 case Primitive::kPrimByte:
3679 case Primitive::kPrimChar:
3680 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003681 case Primitive::kPrimInt: {
3682 locations->SetInAt(0, Location::Any());
3683 break;
3684 }
3685 case Primitive::kPrimLong: {
3686 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3687 if (!instruction->IsConstant()) {
3688 locations->AddTemp(Location::RequiresRegister());
3689 }
3690 break;
3691 }
3692 default:
3693 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3694 }
Calin Juravled0d48522014-11-04 16:40:20 +00003695 if (instruction->HasUses()) {
3696 locations->SetOut(Location::SameAsFirstInput());
3697 }
3698}
3699
3700void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003701 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003702 codegen_->AddSlowPath(slow_path);
3703
3704 LocationSummary* locations = instruction->GetLocations();
3705 Location value = locations->InAt(0);
3706
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003707 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003708 case Primitive::kPrimByte:
3709 case Primitive::kPrimChar:
3710 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003711 case Primitive::kPrimInt: {
3712 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003713 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003714 __ j(kEqual, slow_path->GetEntryLabel());
3715 } else if (value.IsStackSlot()) {
3716 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3717 __ j(kEqual, slow_path->GetEntryLabel());
3718 } else {
3719 DCHECK(value.IsConstant()) << value;
3720 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3721 __ jmp(slow_path->GetEntryLabel());
3722 }
3723 }
3724 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003725 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003726 case Primitive::kPrimLong: {
3727 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003728 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003729 __ movl(temp, value.AsRegisterPairLow<Register>());
3730 __ orl(temp, value.AsRegisterPairHigh<Register>());
3731 __ j(kEqual, slow_path->GetEntryLabel());
3732 } else {
3733 DCHECK(value.IsConstant()) << value;
3734 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3735 __ jmp(slow_path->GetEntryLabel());
3736 }
3737 }
3738 break;
3739 }
3740 default:
3741 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003742 }
Calin Juravled0d48522014-11-04 16:40:20 +00003743}
3744
Calin Juravle9aec02f2014-11-18 23:06:35 +00003745void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3746 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3747
3748 LocationSummary* locations =
3749 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3750
3751 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003752 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003753 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003754 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003755 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003756 // The shift count needs to be in CL or a constant.
3757 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003758 locations->SetOut(Location::SameAsFirstInput());
3759 break;
3760 }
3761 default:
3762 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3763 }
3764}
3765
3766void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3767 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3768
3769 LocationSummary* locations = op->GetLocations();
3770 Location first = locations->InAt(0);
3771 Location second = locations->InAt(1);
3772 DCHECK(first.Equals(locations->Out()));
3773
3774 switch (op->GetResultType()) {
3775 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003776 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003777 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003778 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003779 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003780 DCHECK_EQ(ECX, second_reg);
3781 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003782 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003783 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003784 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003785 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003786 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003787 }
3788 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003789 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3790 if (shift == 0) {
3791 return;
3792 }
3793 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003794 if (op->IsShl()) {
3795 __ shll(first_reg, imm);
3796 } else if (op->IsShr()) {
3797 __ sarl(first_reg, imm);
3798 } else {
3799 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003800 }
3801 }
3802 break;
3803 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003804 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003805 if (second.IsRegister()) {
3806 Register second_reg = second.AsRegister<Register>();
3807 DCHECK_EQ(ECX, second_reg);
3808 if (op->IsShl()) {
3809 GenerateShlLong(first, second_reg);
3810 } else if (op->IsShr()) {
3811 GenerateShrLong(first, second_reg);
3812 } else {
3813 GenerateUShrLong(first, second_reg);
3814 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003815 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003816 // Shift by a constant.
3817 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3818 // Nothing to do if the shift is 0, as the input is already the output.
3819 if (shift != 0) {
3820 if (op->IsShl()) {
3821 GenerateShlLong(first, shift);
3822 } else if (op->IsShr()) {
3823 GenerateShrLong(first, shift);
3824 } else {
3825 GenerateUShrLong(first, shift);
3826 }
3827 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003828 }
3829 break;
3830 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003831 default:
3832 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3833 }
3834}
3835
Mark P Mendell73945692015-04-29 14:56:17 +00003836void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3837 Register low = loc.AsRegisterPairLow<Register>();
3838 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003839 if (shift == 1) {
3840 // This is just an addition.
3841 __ addl(low, low);
3842 __ adcl(high, high);
3843 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003844 // Shift by 32 is easy. High gets low, and low gets 0.
3845 codegen_->EmitParallelMoves(
3846 loc.ToLow(),
3847 loc.ToHigh(),
3848 Primitive::kPrimInt,
3849 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3850 loc.ToLow(),
3851 Primitive::kPrimInt);
3852 } else if (shift > 32) {
3853 // Low part becomes 0. High part is low part << (shift-32).
3854 __ movl(high, low);
3855 __ shll(high, Immediate(shift - 32));
3856 __ xorl(low, low);
3857 } else {
3858 // Between 1 and 31.
3859 __ shld(high, low, Immediate(shift));
3860 __ shll(low, Immediate(shift));
3861 }
3862}
3863
Calin Juravle9aec02f2014-11-18 23:06:35 +00003864void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003865 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003866 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3867 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3868 __ testl(shifter, Immediate(32));
3869 __ j(kEqual, &done);
3870 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3871 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3872 __ Bind(&done);
3873}
3874
Mark P Mendell73945692015-04-29 14:56:17 +00003875void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3876 Register low = loc.AsRegisterPairLow<Register>();
3877 Register high = loc.AsRegisterPairHigh<Register>();
3878 if (shift == 32) {
3879 // Need to copy the sign.
3880 DCHECK_NE(low, high);
3881 __ movl(low, high);
3882 __ sarl(high, Immediate(31));
3883 } else if (shift > 32) {
3884 DCHECK_NE(low, high);
3885 // High part becomes sign. Low part is shifted by shift - 32.
3886 __ movl(low, high);
3887 __ sarl(high, Immediate(31));
3888 __ sarl(low, Immediate(shift - 32));
3889 } else {
3890 // Between 1 and 31.
3891 __ shrd(low, high, Immediate(shift));
3892 __ sarl(high, Immediate(shift));
3893 }
3894}
3895
Calin Juravle9aec02f2014-11-18 23:06:35 +00003896void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003897 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003898 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3899 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3900 __ testl(shifter, Immediate(32));
3901 __ j(kEqual, &done);
3902 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3903 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3904 __ Bind(&done);
3905}
3906
Mark P Mendell73945692015-04-29 14:56:17 +00003907void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3908 Register low = loc.AsRegisterPairLow<Register>();
3909 Register high = loc.AsRegisterPairHigh<Register>();
3910 if (shift == 32) {
3911 // Shift by 32 is easy. Low gets high, and high gets 0.
3912 codegen_->EmitParallelMoves(
3913 loc.ToHigh(),
3914 loc.ToLow(),
3915 Primitive::kPrimInt,
3916 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3917 loc.ToHigh(),
3918 Primitive::kPrimInt);
3919 } else if (shift > 32) {
3920 // Low part is high >> (shift - 32). High part becomes 0.
3921 __ movl(low, high);
3922 __ shrl(low, Immediate(shift - 32));
3923 __ xorl(high, high);
3924 } else {
3925 // Between 1 and 31.
3926 __ shrd(low, high, Immediate(shift));
3927 __ shrl(high, Immediate(shift));
3928 }
3929}
3930
Calin Juravle9aec02f2014-11-18 23:06:35 +00003931void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003932 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003933 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3934 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3935 __ testl(shifter, Immediate(32));
3936 __ j(kEqual, &done);
3937 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3938 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3939 __ Bind(&done);
3940}
3941
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003942void LocationsBuilderX86::VisitRor(HRor* ror) {
3943 LocationSummary* locations =
3944 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3945
3946 switch (ror->GetResultType()) {
3947 case Primitive::kPrimLong:
3948 // Add the temporary needed.
3949 locations->AddTemp(Location::RequiresRegister());
3950 FALLTHROUGH_INTENDED;
3951 case Primitive::kPrimInt:
3952 locations->SetInAt(0, Location::RequiresRegister());
3953 // The shift count needs to be in CL (unless it is a constant).
3954 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3955 locations->SetOut(Location::SameAsFirstInput());
3956 break;
3957 default:
3958 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3959 UNREACHABLE();
3960 }
3961}
3962
3963void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3964 LocationSummary* locations = ror->GetLocations();
3965 Location first = locations->InAt(0);
3966 Location second = locations->InAt(1);
3967
3968 if (ror->GetResultType() == Primitive::kPrimInt) {
3969 Register first_reg = first.AsRegister<Register>();
3970 if (second.IsRegister()) {
3971 Register second_reg = second.AsRegister<Register>();
3972 __ rorl(first_reg, second_reg);
3973 } else {
3974 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3975 __ rorl(first_reg, imm);
3976 }
3977 return;
3978 }
3979
3980 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3981 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3982 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3983 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3984 if (second.IsRegister()) {
3985 Register second_reg = second.AsRegister<Register>();
3986 DCHECK_EQ(second_reg, ECX);
3987 __ movl(temp_reg, first_reg_hi);
3988 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3989 __ shrd(first_reg_lo, temp_reg, second_reg);
3990 __ movl(temp_reg, first_reg_hi);
3991 __ testl(second_reg, Immediate(32));
3992 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3993 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3994 } else {
3995 int32_t shift_amt =
3996 CodeGenerator::GetInt64ValueOf(second.GetConstant()) & kMaxLongShiftValue;
3997 if (shift_amt == 0) {
3998 // Already fine.
3999 return;
4000 }
4001 if (shift_amt == 32) {
4002 // Just swap.
4003 __ movl(temp_reg, first_reg_lo);
4004 __ movl(first_reg_lo, first_reg_hi);
4005 __ movl(first_reg_hi, temp_reg);
4006 return;
4007 }
4008
4009 Immediate imm(shift_amt);
4010 // Save the constents of the low value.
4011 __ movl(temp_reg, first_reg_lo);
4012
4013 // Shift right into low, feeding bits from high.
4014 __ shrd(first_reg_lo, first_reg_hi, imm);
4015
4016 // Shift right into high, feeding bits from the original low.
4017 __ shrd(first_reg_hi, temp_reg, imm);
4018
4019 // Swap if needed.
4020 if (shift_amt > 32) {
4021 __ movl(temp_reg, first_reg_lo);
4022 __ movl(first_reg_lo, first_reg_hi);
4023 __ movl(first_reg_hi, temp_reg);
4024 }
4025 }
4026}
4027
Calin Juravle9aec02f2014-11-18 23:06:35 +00004028void LocationsBuilderX86::VisitShl(HShl* shl) {
4029 HandleShift(shl);
4030}
4031
4032void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4033 HandleShift(shl);
4034}
4035
4036void LocationsBuilderX86::VisitShr(HShr* shr) {
4037 HandleShift(shr);
4038}
4039
4040void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4041 HandleShift(shr);
4042}
4043
4044void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4045 HandleShift(ushr);
4046}
4047
4048void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4049 HandleShift(ushr);
4050}
4051
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004052void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004053 LocationSummary* locations =
4054 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004055 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004056 if (instruction->IsStringAlloc()) {
4057 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4058 } else {
4059 InvokeRuntimeCallingConvention calling_convention;
4060 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4061 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4062 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004063}
4064
4065void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004066 // Note: if heap poisoning is enabled, the entry point takes cares
4067 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004068 if (instruction->IsStringAlloc()) {
4069 // String is allocated through StringFactory. Call NewEmptyString entry point.
4070 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4071 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
4072 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4073 __ call(Address(temp, code_offset.Int32Value()));
4074 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4075 } else {
4076 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4077 instruction,
4078 instruction->GetDexPc(),
4079 nullptr);
4080 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4081 DCHECK(!codegen_->IsLeafMethod());
4082 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004083}
4084
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004085void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4086 LocationSummary* locations =
4087 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4088 locations->SetOut(Location::RegisterLocation(EAX));
4089 InvokeRuntimeCallingConvention calling_convention;
4090 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004091 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004092 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004093}
4094
4095void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4096 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004097 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004098 // Note: if heap poisoning is enabled, the entry point takes cares
4099 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004100 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4101 instruction,
4102 instruction->GetDexPc(),
4103 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004104 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004105 DCHECK(!codegen_->IsLeafMethod());
4106}
4107
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004108void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004109 LocationSummary* locations =
4110 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004111 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4112 if (location.IsStackSlot()) {
4113 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4114 } else if (location.IsDoubleStackSlot()) {
4115 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004116 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004117 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004118}
4119
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004120void InstructionCodeGeneratorX86::VisitParameterValue(
4121 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4122}
4123
4124void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4125 LocationSummary* locations =
4126 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4127 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4128}
4129
4130void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004131}
4132
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004133void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4134 LocationSummary* locations =
4135 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4136 locations->SetInAt(0, Location::RequiresRegister());
4137 locations->SetOut(Location::RequiresRegister());
4138}
4139
4140void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4141 LocationSummary* locations = instruction->GetLocations();
4142 uint32_t method_offset = 0;
4143 if (instruction->GetTableKind() == HClassTableGet::kVTable) {
4144 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4145 instruction->GetIndex(), kX86PointerSize).SizeValue();
4146 } else {
4147 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
4148 instruction->GetIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
4149 }
4150 __ movl(locations->Out().AsRegister<Register>(),
4151 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
4152}
4153
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004154void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004155 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004156 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004157 locations->SetInAt(0, Location::RequiresRegister());
4158 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004159}
4160
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004161void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4162 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004163 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004164 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004165 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004166 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004167 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004168 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004169 break;
4170
4171 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004172 __ notl(out.AsRegisterPairLow<Register>());
4173 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004174 break;
4175
4176 default:
4177 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4178 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004179}
4180
David Brazdil66d126e2015-04-03 16:02:44 +01004181void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4182 LocationSummary* locations =
4183 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4184 locations->SetInAt(0, Location::RequiresRegister());
4185 locations->SetOut(Location::SameAsFirstInput());
4186}
4187
4188void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004189 LocationSummary* locations = bool_not->GetLocations();
4190 Location in = locations->InAt(0);
4191 Location out = locations->Out();
4192 DCHECK(in.Equals(out));
4193 __ xorl(out.AsRegister<Register>(), Immediate(1));
4194}
4195
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004196void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004197 LocationSummary* locations =
4198 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004199 switch (compare->InputAt(0)->GetType()) {
Aart Bika19616e2016-02-01 18:57:58 -08004200 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004201 case Primitive::kPrimLong: {
4202 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004203 locations->SetInAt(1, Location::Any());
4204 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4205 break;
4206 }
4207 case Primitive::kPrimFloat:
4208 case Primitive::kPrimDouble: {
4209 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004210 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4211 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4212 } else if (compare->InputAt(1)->IsConstant()) {
4213 locations->SetInAt(1, Location::RequiresFpuRegister());
4214 } else {
4215 locations->SetInAt(1, Location::Any());
4216 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004217 locations->SetOut(Location::RequiresRegister());
4218 break;
4219 }
4220 default:
4221 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4222 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004223}
4224
4225void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004226 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004227 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004228 Location left = locations->InAt(0);
4229 Location right = locations->InAt(1);
4230
Mark Mendell0c9497d2015-08-21 09:30:05 -04004231 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004232 Condition less_cond = kLess;
4233
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004234 switch (compare->InputAt(0)->GetType()) {
Aart Bika19616e2016-02-01 18:57:58 -08004235 case Primitive::kPrimInt: {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05004236 GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004237 break;
4238 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004239 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004240 Register left_low = left.AsRegisterPairLow<Register>();
4241 Register left_high = left.AsRegisterPairHigh<Register>();
4242 int32_t val_low = 0;
4243 int32_t val_high = 0;
4244 bool right_is_const = false;
4245
4246 if (right.IsConstant()) {
4247 DCHECK(right.GetConstant()->IsLongConstant());
4248 right_is_const = true;
4249 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4250 val_low = Low32Bits(val);
4251 val_high = High32Bits(val);
4252 }
4253
Calin Juravleddb7df22014-11-25 20:56:51 +00004254 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004255 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004256 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004257 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004258 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004259 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004260 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004261 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004262 __ j(kLess, &less); // Signed compare.
4263 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004264 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004265 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004266 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004267 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004268 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004269 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004270 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004271 }
Aart Bika19616e2016-02-01 18:57:58 -08004272 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004273 break;
4274 }
4275 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004276 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004277 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004278 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004279 break;
4280 }
4281 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004282 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004283 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004284 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004285 break;
4286 }
4287 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004288 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004289 }
Aart Bika19616e2016-02-01 18:57:58 -08004290
Calin Juravleddb7df22014-11-25 20:56:51 +00004291 __ movl(out, Immediate(0));
4292 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004293 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004294
4295 __ Bind(&greater);
4296 __ movl(out, Immediate(1));
4297 __ jmp(&done);
4298
4299 __ Bind(&less);
4300 __ movl(out, Immediate(-1));
4301
4302 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004303}
4304
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004305void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004306 LocationSummary* locations =
4307 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004308 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4309 locations->SetInAt(i, Location::Any());
4310 }
4311 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004312}
4313
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004314void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004315 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004316}
4317
Roland Levillain7c1559a2015-12-15 10:55:36 +00004318void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004319 /*
4320 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4321 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4322 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4323 */
4324 switch (kind) {
4325 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004326 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004327 break;
4328 }
4329 case MemBarrierKind::kAnyStore:
4330 case MemBarrierKind::kLoadAny:
4331 case MemBarrierKind::kStoreStore: {
4332 // nop
4333 break;
4334 }
4335 default:
4336 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004337 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004338}
4339
Vladimir Markodc151b22015-10-15 18:02:30 +01004340HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4341 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4342 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004343 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4344
4345 // We disable pc-relative load when there is an irreducible loop, as the optimization
4346 // is incompatible with it.
4347 if (GetGraph()->HasIrreducibleLoops() &&
4348 (dispatch_info.method_load_kind ==
4349 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4350 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4351 }
4352 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004353 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4354 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4355 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4356 // (Though the direct CALL ptr16:32 is available for consideration).
4357 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004358 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004359 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004360 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004361 0u
4362 };
4363 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004364 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004365 }
4366}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004367
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004368Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4369 Register temp) {
4370 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004371 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004372 if (!invoke->GetLocations()->Intrinsified()) {
4373 return location.AsRegister<Register>();
4374 }
4375 // For intrinsics we allow any location, so it may be on the stack.
4376 if (!location.IsRegister()) {
4377 __ movl(temp, Address(ESP, location.GetStackIndex()));
4378 return temp;
4379 }
4380 // For register locations, check if the register was saved. If so, get it from the stack.
4381 // Note: There is a chance that the register was saved but not overwritten, so we could
4382 // save one load. However, since this is just an intrinsic slow path we prefer this
4383 // simple and more robust approach rather that trying to determine if that's the case.
4384 SlowPathCode* slow_path = GetCurrentSlowPath();
4385 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4386 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4387 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4388 __ movl(temp, Address(ESP, stack_offset));
4389 return temp;
4390 }
4391 return location.AsRegister<Register>();
4392}
4393
Vladimir Marko58155012015-08-19 12:49:41 +00004394void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4395 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4396 switch (invoke->GetMethodLoadKind()) {
4397 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4398 // temp = thread->string_init_entrypoint
4399 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4400 break;
4401 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004402 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004403 break;
4404 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4405 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4406 break;
4407 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4408 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4409 method_patches_.emplace_back(invoke->GetTargetMethod());
4410 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4411 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004412 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4413 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4414 temp.AsRegister<Register>());
4415 uint32_t offset = invoke->GetDexCacheArrayOffset();
4416 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4417 // Add the patch entry and bind its label at the end of the instruction.
4418 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4419 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4420 break;
4421 }
Vladimir Marko58155012015-08-19 12:49:41 +00004422 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004423 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004424 Register method_reg;
4425 Register reg = temp.AsRegister<Register>();
4426 if (current_method.IsRegister()) {
4427 method_reg = current_method.AsRegister<Register>();
4428 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004429 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004430 DCHECK(!current_method.IsValid());
4431 method_reg = reg;
4432 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4433 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004434 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004435 __ movl(reg, Address(method_reg,
4436 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004437 // temp = temp[index_in_cache]
4438 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4439 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4440 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004441 }
Vladimir Marko58155012015-08-19 12:49:41 +00004442 }
4443
4444 switch (invoke->GetCodePtrLocation()) {
4445 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4446 __ call(GetFrameEntryLabel());
4447 break;
4448 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4449 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4450 Label* label = &relative_call_patches_.back().label;
4451 __ call(label); // Bind to the patch label, override at link time.
4452 __ Bind(label); // Bind the label at the end of the "call" insn.
4453 break;
4454 }
4455 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4456 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004457 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4458 LOG(FATAL) << "Unsupported";
4459 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004460 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4461 // (callee_method + offset_of_quick_compiled_code)()
4462 __ call(Address(callee_method.AsRegister<Register>(),
4463 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4464 kX86WordSize).Int32Value()));
4465 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004466 }
4467
4468 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004469}
4470
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004471void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4472 Register temp = temp_in.AsRegister<Register>();
4473 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4474 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004475
4476 // Use the calling convention instead of the location of the receiver, as
4477 // intrinsics may have put the receiver in a different register. In the intrinsics
4478 // slow path, the arguments have been moved to the right place, so here we are
4479 // guaranteed that the receiver is the first register of the calling convention.
4480 InvokeDexCallingConvention calling_convention;
4481 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004482 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004483 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004484 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004485 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004486 // Instead of simply (possibly) unpoisoning `temp` here, we should
4487 // emit a read barrier for the previous class reference load.
4488 // However this is not required in practice, as this is an
4489 // intermediate/temporary reference and because the current
4490 // concurrent copying collector keeps the from-space memory
4491 // intact/accessible until the end of the marking phase (the
4492 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004493 __ MaybeUnpoisonHeapReference(temp);
4494 // temp = temp->GetMethodAt(method_offset);
4495 __ movl(temp, Address(temp, method_offset));
4496 // call temp->GetEntryPoint();
4497 __ call(Address(
4498 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4499}
4500
Vladimir Marko58155012015-08-19 12:49:41 +00004501void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4502 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004503 size_t size =
4504 method_patches_.size() +
4505 relative_call_patches_.size() +
4506 pc_relative_dex_cache_patches_.size();
4507 linker_patches->reserve(size);
4508 // The label points to the end of the "movl" insn but the literal offset for method
4509 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4510 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004511 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004512 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004513 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4514 info.target_method.dex_file,
4515 info.target_method.dex_method_index));
4516 }
4517 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004518 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004519 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4520 info.target_method.dex_file,
4521 info.target_method.dex_method_index));
4522 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004523 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4524 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4525 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4526 &info.target_dex_file,
4527 GetMethodAddressOffset(),
4528 info.element_offset));
4529 }
Vladimir Marko58155012015-08-19 12:49:41 +00004530}
4531
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004532void CodeGeneratorX86::MarkGCCard(Register temp,
4533 Register card,
4534 Register object,
4535 Register value,
4536 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004537 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004538 if (value_can_be_null) {
4539 __ testl(value, value);
4540 __ j(kEqual, &is_null);
4541 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004542 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4543 __ movl(temp, object);
4544 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004545 __ movb(Address(temp, card, TIMES_1, 0),
4546 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004547 if (value_can_be_null) {
4548 __ Bind(&is_null);
4549 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004550}
4551
Calin Juravle52c48962014-12-16 17:02:57 +00004552void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4553 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004554
4555 bool object_field_get_with_read_barrier =
4556 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004557 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004558 new (GetGraph()->GetArena()) LocationSummary(instruction,
4559 kEmitCompilerReadBarrier ?
4560 LocationSummary::kCallOnSlowPath :
4561 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004562 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004563
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004564 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4565 locations->SetOut(Location::RequiresFpuRegister());
4566 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004567 // The output overlaps in case of long: we don't want the low move
4568 // to overwrite the object's location. Likewise, in the case of
4569 // an object field get with read barriers enabled, we do not want
4570 // the move to overwrite the object's location, as we need it to emit
4571 // the read barrier.
4572 locations->SetOut(
4573 Location::RequiresRegister(),
4574 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4575 Location::kOutputOverlap :
4576 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004577 }
Calin Juravle52c48962014-12-16 17:02:57 +00004578
4579 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4580 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004581 // So we use an XMM register as a temp to achieve atomicity (first
4582 // load the temp into the XMM and then copy the XMM into the
4583 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004584 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004585 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4586 // We need a temporary register for the read barrier marking slow
4587 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4588 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004589 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004590}
4591
Calin Juravle52c48962014-12-16 17:02:57 +00004592void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4593 const FieldInfo& field_info) {
4594 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004595
Calin Juravle52c48962014-12-16 17:02:57 +00004596 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004597 Location base_loc = locations->InAt(0);
4598 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004599 Location out = locations->Out();
4600 bool is_volatile = field_info.IsVolatile();
4601 Primitive::Type field_type = field_info.GetFieldType();
4602 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4603
4604 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004605 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004606 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004607 break;
4608 }
4609
4610 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004611 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004612 break;
4613 }
4614
4615 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004616 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004617 break;
4618 }
4619
4620 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004621 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004622 break;
4623 }
4624
4625 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004626 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004627 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004628
4629 case Primitive::kPrimNot: {
4630 // /* HeapReference<Object> */ out = *(base + offset)
4631 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4632 Location temp_loc = locations->GetTemp(0);
4633 // Note that a potential implicit null check is handled in this
4634 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4635 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4636 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4637 if (is_volatile) {
4638 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4639 }
4640 } else {
4641 __ movl(out.AsRegister<Register>(), Address(base, offset));
4642 codegen_->MaybeRecordImplicitNullCheck(instruction);
4643 if (is_volatile) {
4644 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4645 }
4646 // If read barriers are enabled, emit read barriers other than
4647 // Baker's using a slow path (and also unpoison the loaded
4648 // reference, if heap poisoning is enabled).
4649 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4650 }
4651 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004652 }
4653
4654 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004655 if (is_volatile) {
4656 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4657 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004658 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004659 __ movd(out.AsRegisterPairLow<Register>(), temp);
4660 __ psrlq(temp, Immediate(32));
4661 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4662 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004663 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004664 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004665 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004666 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4667 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004668 break;
4669 }
4670
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004671 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004672 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004673 break;
4674 }
4675
4676 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004677 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004678 break;
4679 }
4680
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004681 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004682 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004683 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004684 }
Calin Juravle52c48962014-12-16 17:02:57 +00004685
Roland Levillain7c1559a2015-12-15 10:55:36 +00004686 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4687 // Potential implicit null checks, in the case of reference or
4688 // long fields, are handled in the previous switch statement.
4689 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004690 codegen_->MaybeRecordImplicitNullCheck(instruction);
4691 }
4692
Calin Juravle52c48962014-12-16 17:02:57 +00004693 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004694 if (field_type == Primitive::kPrimNot) {
4695 // Memory barriers, in the case of references, are also handled
4696 // in the previous switch statement.
4697 } else {
4698 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4699 }
Roland Levillain4d027112015-07-01 15:41:14 +01004700 }
Calin Juravle52c48962014-12-16 17:02:57 +00004701}
4702
4703void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4704 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4705
4706 LocationSummary* locations =
4707 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4708 locations->SetInAt(0, Location::RequiresRegister());
4709 bool is_volatile = field_info.IsVolatile();
4710 Primitive::Type field_type = field_info.GetFieldType();
4711 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4712 || (field_type == Primitive::kPrimByte);
4713
4714 // The register allocator does not support multiple
4715 // inputs that die at entry with one in a specific register.
4716 if (is_byte_type) {
4717 // Ensure the value is in a byte register.
4718 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004719 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004720 if (is_volatile && field_type == Primitive::kPrimDouble) {
4721 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4722 locations->SetInAt(1, Location::RequiresFpuRegister());
4723 } else {
4724 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4725 }
4726 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4727 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004728 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004729
Calin Juravle52c48962014-12-16 17:02:57 +00004730 // 64bits value can be atomically written to an address with movsd and an XMM register.
4731 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4732 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4733 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4734 // isolated cases when we need this it isn't worth adding the extra complexity.
4735 locations->AddTemp(Location::RequiresFpuRegister());
4736 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004737 } else {
4738 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4739
4740 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4741 // Temporary registers for the write barrier.
4742 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4743 // Ensure the card is in a byte register.
4744 locations->AddTemp(Location::RegisterLocation(ECX));
4745 }
Calin Juravle52c48962014-12-16 17:02:57 +00004746 }
4747}
4748
4749void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004750 const FieldInfo& field_info,
4751 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004752 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4753
4754 LocationSummary* locations = instruction->GetLocations();
4755 Register base = locations->InAt(0).AsRegister<Register>();
4756 Location value = locations->InAt(1);
4757 bool is_volatile = field_info.IsVolatile();
4758 Primitive::Type field_type = field_info.GetFieldType();
4759 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004760 bool needs_write_barrier =
4761 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004762
4763 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004764 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004765 }
4766
Mark Mendell81489372015-11-04 11:30:41 -05004767 bool maybe_record_implicit_null_check_done = false;
4768
Calin Juravle52c48962014-12-16 17:02:57 +00004769 switch (field_type) {
4770 case Primitive::kPrimBoolean:
4771 case Primitive::kPrimByte: {
4772 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4773 break;
4774 }
4775
4776 case Primitive::kPrimShort:
4777 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004778 if (value.IsConstant()) {
4779 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4780 __ movw(Address(base, offset), Immediate(v));
4781 } else {
4782 __ movw(Address(base, offset), value.AsRegister<Register>());
4783 }
Calin Juravle52c48962014-12-16 17:02:57 +00004784 break;
4785 }
4786
4787 case Primitive::kPrimInt:
4788 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004789 if (kPoisonHeapReferences && needs_write_barrier) {
4790 // Note that in the case where `value` is a null reference,
4791 // we do not enter this block, as the reference does not
4792 // need poisoning.
4793 DCHECK_EQ(field_type, Primitive::kPrimNot);
4794 Register temp = locations->GetTemp(0).AsRegister<Register>();
4795 __ movl(temp, value.AsRegister<Register>());
4796 __ PoisonHeapReference(temp);
4797 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004798 } else if (value.IsConstant()) {
4799 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4800 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004801 } else {
4802 __ movl(Address(base, offset), value.AsRegister<Register>());
4803 }
Calin Juravle52c48962014-12-16 17:02:57 +00004804 break;
4805 }
4806
4807 case Primitive::kPrimLong: {
4808 if (is_volatile) {
4809 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4810 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4811 __ movd(temp1, value.AsRegisterPairLow<Register>());
4812 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4813 __ punpckldq(temp1, temp2);
4814 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004815 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004816 } else if (value.IsConstant()) {
4817 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4818 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4819 codegen_->MaybeRecordImplicitNullCheck(instruction);
4820 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004821 } else {
4822 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004823 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004824 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4825 }
Mark Mendell81489372015-11-04 11:30:41 -05004826 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004827 break;
4828 }
4829
4830 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004831 if (value.IsConstant()) {
4832 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4833 __ movl(Address(base, offset), Immediate(v));
4834 } else {
4835 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4836 }
Calin Juravle52c48962014-12-16 17:02:57 +00004837 break;
4838 }
4839
4840 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004841 if (value.IsConstant()) {
4842 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4843 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4844 codegen_->MaybeRecordImplicitNullCheck(instruction);
4845 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4846 maybe_record_implicit_null_check_done = true;
4847 } else {
4848 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4849 }
Calin Juravle52c48962014-12-16 17:02:57 +00004850 break;
4851 }
4852
4853 case Primitive::kPrimVoid:
4854 LOG(FATAL) << "Unreachable type " << field_type;
4855 UNREACHABLE();
4856 }
4857
Mark Mendell81489372015-11-04 11:30:41 -05004858 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004859 codegen_->MaybeRecordImplicitNullCheck(instruction);
4860 }
4861
Roland Levillain4d027112015-07-01 15:41:14 +01004862 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004863 Register temp = locations->GetTemp(0).AsRegister<Register>();
4864 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004865 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004866 }
4867
Calin Juravle52c48962014-12-16 17:02:57 +00004868 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004869 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004870 }
4871}
4872
4873void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4874 HandleFieldGet(instruction, instruction->GetFieldInfo());
4875}
4876
4877void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4878 HandleFieldGet(instruction, instruction->GetFieldInfo());
4879}
4880
4881void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4882 HandleFieldSet(instruction, instruction->GetFieldInfo());
4883}
4884
4885void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004886 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004887}
4888
4889void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4890 HandleFieldSet(instruction, instruction->GetFieldInfo());
4891}
4892
4893void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004894 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004895}
4896
4897void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4898 HandleFieldGet(instruction, instruction->GetFieldInfo());
4899}
4900
4901void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4902 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004903}
4904
Calin Juravlee460d1d2015-09-29 04:52:17 +01004905void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4906 HUnresolvedInstanceFieldGet* instruction) {
4907 FieldAccessCallingConventionX86 calling_convention;
4908 codegen_->CreateUnresolvedFieldLocationSummary(
4909 instruction, instruction->GetFieldType(), calling_convention);
4910}
4911
4912void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4913 HUnresolvedInstanceFieldGet* instruction) {
4914 FieldAccessCallingConventionX86 calling_convention;
4915 codegen_->GenerateUnresolvedFieldAccess(instruction,
4916 instruction->GetFieldType(),
4917 instruction->GetFieldIndex(),
4918 instruction->GetDexPc(),
4919 calling_convention);
4920}
4921
4922void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4923 HUnresolvedInstanceFieldSet* instruction) {
4924 FieldAccessCallingConventionX86 calling_convention;
4925 codegen_->CreateUnresolvedFieldLocationSummary(
4926 instruction, instruction->GetFieldType(), calling_convention);
4927}
4928
4929void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4930 HUnresolvedInstanceFieldSet* instruction) {
4931 FieldAccessCallingConventionX86 calling_convention;
4932 codegen_->GenerateUnresolvedFieldAccess(instruction,
4933 instruction->GetFieldType(),
4934 instruction->GetFieldIndex(),
4935 instruction->GetDexPc(),
4936 calling_convention);
4937}
4938
4939void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4940 HUnresolvedStaticFieldGet* instruction) {
4941 FieldAccessCallingConventionX86 calling_convention;
4942 codegen_->CreateUnresolvedFieldLocationSummary(
4943 instruction, instruction->GetFieldType(), calling_convention);
4944}
4945
4946void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4947 HUnresolvedStaticFieldGet* instruction) {
4948 FieldAccessCallingConventionX86 calling_convention;
4949 codegen_->GenerateUnresolvedFieldAccess(instruction,
4950 instruction->GetFieldType(),
4951 instruction->GetFieldIndex(),
4952 instruction->GetDexPc(),
4953 calling_convention);
4954}
4955
4956void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4957 HUnresolvedStaticFieldSet* instruction) {
4958 FieldAccessCallingConventionX86 calling_convention;
4959 codegen_->CreateUnresolvedFieldLocationSummary(
4960 instruction, instruction->GetFieldType(), calling_convention);
4961}
4962
4963void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4964 HUnresolvedStaticFieldSet* instruction) {
4965 FieldAccessCallingConventionX86 calling_convention;
4966 codegen_->GenerateUnresolvedFieldAccess(instruction,
4967 instruction->GetFieldType(),
4968 instruction->GetFieldIndex(),
4969 instruction->GetDexPc(),
4970 calling_convention);
4971}
4972
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004973void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004974 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4975 ? LocationSummary::kCallOnSlowPath
4976 : LocationSummary::kNoCall;
4977 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4978 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004979 ? Location::RequiresRegister()
4980 : Location::Any();
4981 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004982 if (instruction->HasUses()) {
4983 locations->SetOut(Location::SameAsFirstInput());
4984 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004985}
4986
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004987void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004988 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4989 return;
4990 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004991 LocationSummary* locations = instruction->GetLocations();
4992 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004993
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004994 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4995 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4996}
4997
4998void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004999 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005000 codegen_->AddSlowPath(slow_path);
5001
5002 LocationSummary* locations = instruction->GetLocations();
5003 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005004
5005 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005006 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005007 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005008 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005009 } else {
5010 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005011 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005012 __ jmp(slow_path->GetEntryLabel());
5013 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005014 }
5015 __ j(kEqual, slow_path->GetEntryLabel());
5016}
5017
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005018void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005019 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005020 GenerateImplicitNullCheck(instruction);
5021 } else {
5022 GenerateExplicitNullCheck(instruction);
5023 }
5024}
5025
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005026void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005027 bool object_array_get_with_read_barrier =
5028 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005029 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005030 new (GetGraph()->GetArena()) LocationSummary(instruction,
5031 object_array_get_with_read_barrier ?
5032 LocationSummary::kCallOnSlowPath :
5033 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005034 locations->SetInAt(0, Location::RequiresRegister());
5035 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005036 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5037 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5038 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005039 // The output overlaps in case of long: we don't want the low move
5040 // to overwrite the array's location. Likewise, in the case of an
5041 // object array get with read barriers enabled, we do not want the
5042 // move to overwrite the array's location, as we need it to emit
5043 // the read barrier.
5044 locations->SetOut(
5045 Location::RequiresRegister(),
5046 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5047 Location::kOutputOverlap :
5048 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005049 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005050 // We need a temporary register for the read barrier marking slow
5051 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5052 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5053 locations->AddTemp(Location::RequiresRegister());
5054 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005055}
5056
5057void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5058 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005059 Location obj_loc = locations->InAt(0);
5060 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005061 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005062 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005063
Calin Juravle77520bc2015-01-12 18:45:46 +00005064 Primitive::Type type = instruction->GetType();
5065 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005066 case Primitive::kPrimBoolean: {
5067 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005068 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005069 if (index.IsConstant()) {
5070 __ movzxb(out, Address(obj,
5071 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5072 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005073 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005074 }
5075 break;
5076 }
5077
5078 case Primitive::kPrimByte: {
5079 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005080 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005081 if (index.IsConstant()) {
5082 __ movsxb(out, Address(obj,
5083 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5084 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005085 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005086 }
5087 break;
5088 }
5089
5090 case Primitive::kPrimShort: {
5091 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005092 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005093 if (index.IsConstant()) {
5094 __ movsxw(out, Address(obj,
5095 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5096 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005097 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005098 }
5099 break;
5100 }
5101
5102 case Primitive::kPrimChar: {
5103 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005104 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005105 if (index.IsConstant()) {
5106 __ movzxw(out, Address(obj,
5107 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5108 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005109 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110 }
5111 break;
5112 }
5113
Roland Levillain7c1559a2015-12-15 10:55:36 +00005114 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005115 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005116 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005117 if (index.IsConstant()) {
5118 __ movl(out, Address(obj,
5119 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5120 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005121 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005122 }
5123 break;
5124 }
5125
Roland Levillain7c1559a2015-12-15 10:55:36 +00005126 case Primitive::kPrimNot: {
5127 static_assert(
5128 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5129 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5130 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5131 // /* HeapReference<Object> */ out =
5132 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5133 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5134 Location temp = locations->GetTemp(0);
5135 // Note that a potential implicit null check is handled in this
5136 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5137 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5138 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5139 } else {
5140 Register out = out_loc.AsRegister<Register>();
5141 if (index.IsConstant()) {
5142 uint32_t offset =
5143 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5144 __ movl(out, Address(obj, offset));
5145 codegen_->MaybeRecordImplicitNullCheck(instruction);
5146 // If read barriers are enabled, emit read barriers other than
5147 // Baker's using a slow path (and also unpoison the loaded
5148 // reference, if heap poisoning is enabled).
5149 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5150 } else {
5151 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5152 codegen_->MaybeRecordImplicitNullCheck(instruction);
5153 // If read barriers are enabled, emit read barriers other than
5154 // Baker's using a slow path (and also unpoison the loaded
5155 // reference, if heap poisoning is enabled).
5156 codegen_->MaybeGenerateReadBarrierSlow(
5157 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5158 }
5159 }
5160 break;
5161 }
5162
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005163 case Primitive::kPrimLong: {
5164 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005165 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005166 if (index.IsConstant()) {
5167 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005168 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005169 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005170 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005171 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005172 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005173 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005174 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005175 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005176 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005177 }
5178 break;
5179 }
5180
Mark Mendell7c8d0092015-01-26 11:21:33 -05005181 case Primitive::kPrimFloat: {
5182 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005183 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005184 if (index.IsConstant()) {
5185 __ movss(out, Address(obj,
5186 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5187 } else {
5188 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5189 }
5190 break;
5191 }
5192
5193 case Primitive::kPrimDouble: {
5194 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005195 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005196 if (index.IsConstant()) {
5197 __ movsd(out, Address(obj,
5198 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5199 } else {
5200 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5201 }
5202 break;
5203 }
5204
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005205 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005206 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005207 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005208 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005209
Roland Levillain7c1559a2015-12-15 10:55:36 +00005210 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5211 // Potential implicit null checks, in the case of reference or
5212 // long arrays, are handled in the previous switch statement.
5213 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005214 codegen_->MaybeRecordImplicitNullCheck(instruction);
5215 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005216}
5217
5218void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005219 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005220
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005221 bool needs_write_barrier =
5222 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005223 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5224 bool object_array_set_with_read_barrier =
5225 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005226
Nicolas Geoffray39468442014-09-02 15:17:15 +01005227 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5228 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005229 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5230 LocationSummary::kCallOnSlowPath :
5231 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005232
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005233 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5234 || (value_type == Primitive::kPrimByte);
5235 // We need the inputs to be different than the output in case of long operation.
5236 // In case of a byte operation, the register allocator does not support multiple
5237 // inputs that die at entry with one in a specific register.
5238 locations->SetInAt(0, Location::RequiresRegister());
5239 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5240 if (is_byte_type) {
5241 // Ensure the value is in a byte register.
5242 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5243 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005244 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005245 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005246 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5247 }
5248 if (needs_write_barrier) {
5249 // Temporary registers for the write barrier.
5250 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5251 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005252 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005253 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254}
5255
5256void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5257 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005258 Location array_loc = locations->InAt(0);
5259 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005261 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005262 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005263 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5264 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5265 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005266 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005267 bool needs_write_barrier =
5268 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005269
5270 switch (value_type) {
5271 case Primitive::kPrimBoolean:
5272 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005273 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5274 Address address = index.IsConstant()
5275 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5276 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5277 if (value.IsRegister()) {
5278 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005279 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005280 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005281 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005282 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005283 break;
5284 }
5285
5286 case Primitive::kPrimShort:
5287 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005288 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5289 Address address = index.IsConstant()
5290 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5291 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5292 if (value.IsRegister()) {
5293 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005294 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005295 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005296 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005297 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005298 break;
5299 }
5300
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005301 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005302 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5303 Address address = index.IsConstant()
5304 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5305 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005306
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005307 if (!value.IsRegister()) {
5308 // Just setting null.
5309 DCHECK(instruction->InputAt(2)->IsNullConstant());
5310 DCHECK(value.IsConstant()) << value;
5311 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005312 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005313 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005314 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005315 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005316 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005317
5318 DCHECK(needs_write_barrier);
5319 Register register_value = value.AsRegister<Register>();
5320 NearLabel done, not_null, do_put;
5321 SlowPathCode* slow_path = nullptr;
5322 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005323 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005324 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5325 codegen_->AddSlowPath(slow_path);
5326 if (instruction->GetValueCanBeNull()) {
5327 __ testl(register_value, register_value);
5328 __ j(kNotEqual, &not_null);
5329 __ movl(address, Immediate(0));
5330 codegen_->MaybeRecordImplicitNullCheck(instruction);
5331 __ jmp(&done);
5332 __ Bind(&not_null);
5333 }
5334
Roland Levillain0d5a2812015-11-13 10:07:31 +00005335 if (kEmitCompilerReadBarrier) {
5336 // When read barriers are enabled, the type checking
5337 // instrumentation requires two read barriers:
5338 //
5339 // __ movl(temp2, temp);
5340 // // /* HeapReference<Class> */ temp = temp->component_type_
5341 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005342 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005343 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5344 //
5345 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5346 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005347 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005348 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5349 //
5350 // __ cmpl(temp, temp2);
5351 //
5352 // However, the second read barrier may trash `temp`, as it
5353 // is a temporary register, and as such would not be saved
5354 // along with live registers before calling the runtime (nor
5355 // restored afterwards). So in this case, we bail out and
5356 // delegate the work to the array set slow path.
5357 //
5358 // TODO: Extend the register allocator to support a new
5359 // "(locally) live temp" location so as to avoid always
5360 // going into the slow path when read barriers are enabled.
5361 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005362 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005363 // /* HeapReference<Class> */ temp = array->klass_
5364 __ movl(temp, Address(array, class_offset));
5365 codegen_->MaybeRecordImplicitNullCheck(instruction);
5366 __ MaybeUnpoisonHeapReference(temp);
5367
5368 // /* HeapReference<Class> */ temp = temp->component_type_
5369 __ movl(temp, Address(temp, component_offset));
5370 // If heap poisoning is enabled, no need to unpoison `temp`
5371 // nor the object reference in `register_value->klass`, as
5372 // we are comparing two poisoned references.
5373 __ cmpl(temp, Address(register_value, class_offset));
5374
5375 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5376 __ j(kEqual, &do_put);
5377 // If heap poisoning is enabled, the `temp` reference has
5378 // not been unpoisoned yet; unpoison it now.
5379 __ MaybeUnpoisonHeapReference(temp);
5380
5381 // /* HeapReference<Class> */ temp = temp->super_class_
5382 __ movl(temp, Address(temp, super_offset));
5383 // If heap poisoning is enabled, no need to unpoison
5384 // `temp`, as we are comparing against null below.
5385 __ testl(temp, temp);
5386 __ j(kNotEqual, slow_path->GetEntryLabel());
5387 __ Bind(&do_put);
5388 } else {
5389 __ j(kNotEqual, slow_path->GetEntryLabel());
5390 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005391 }
5392 }
5393
5394 if (kPoisonHeapReferences) {
5395 __ movl(temp, register_value);
5396 __ PoisonHeapReference(temp);
5397 __ movl(address, temp);
5398 } else {
5399 __ movl(address, register_value);
5400 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005401 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005402 codegen_->MaybeRecordImplicitNullCheck(instruction);
5403 }
5404
5405 Register card = locations->GetTemp(1).AsRegister<Register>();
5406 codegen_->MarkGCCard(
5407 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5408 __ Bind(&done);
5409
5410 if (slow_path != nullptr) {
5411 __ Bind(slow_path->GetExitLabel());
5412 }
5413
5414 break;
5415 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005416
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005417 case Primitive::kPrimInt: {
5418 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5419 Address address = index.IsConstant()
5420 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5421 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5422 if (value.IsRegister()) {
5423 __ movl(address, value.AsRegister<Register>());
5424 } else {
5425 DCHECK(value.IsConstant()) << value;
5426 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5427 __ movl(address, Immediate(v));
5428 }
5429 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005430 break;
5431 }
5432
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005433 case Primitive::kPrimLong: {
5434 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005435 if (index.IsConstant()) {
5436 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005437 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005438 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005439 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005440 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005441 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005442 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005443 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005444 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005445 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005446 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005447 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005448 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005449 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005450 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005451 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005452 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005453 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005454 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005455 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005456 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005457 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005458 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005459 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005460 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005461 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005462 Immediate(High32Bits(val)));
5463 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005464 }
5465 break;
5466 }
5467
Mark Mendell7c8d0092015-01-26 11:21:33 -05005468 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005469 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5470 Address address = index.IsConstant()
5471 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5472 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005473 if (value.IsFpuRegister()) {
5474 __ movss(address, value.AsFpuRegister<XmmRegister>());
5475 } else {
5476 DCHECK(value.IsConstant());
5477 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5478 __ movl(address, Immediate(v));
5479 }
5480 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005481 break;
5482 }
5483
5484 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005485 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5486 Address address = index.IsConstant()
5487 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5488 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005489 if (value.IsFpuRegister()) {
5490 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5491 } else {
5492 DCHECK(value.IsConstant());
5493 Address address_hi = index.IsConstant() ?
5494 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5495 offset + kX86WordSize) :
5496 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5497 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5498 __ movl(address, Immediate(Low32Bits(v)));
5499 codegen_->MaybeRecordImplicitNullCheck(instruction);
5500 __ movl(address_hi, Immediate(High32Bits(v)));
5501 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005502 break;
5503 }
5504
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005505 case Primitive::kPrimVoid:
5506 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005507 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005508 }
5509}
5510
5511void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5512 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005513 locations->SetInAt(0, Location::RequiresRegister());
5514 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005515}
5516
5517void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5518 LocationSummary* locations = instruction->GetLocations();
5519 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005520 Register obj = locations->InAt(0).AsRegister<Register>();
5521 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005522 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005523 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005524}
5525
5526void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005527 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5528 ? LocationSummary::kCallOnSlowPath
5529 : LocationSummary::kNoCall;
5530 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005531 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005532 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005533 if (instruction->HasUses()) {
5534 locations->SetOut(Location::SameAsFirstInput());
5535 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005536}
5537
5538void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5539 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005540 Location index_loc = locations->InAt(0);
5541 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005542 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005543 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005544
Mark Mendell99dbd682015-04-22 16:18:52 -04005545 if (length_loc.IsConstant()) {
5546 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5547 if (index_loc.IsConstant()) {
5548 // BCE will remove the bounds check if we are guarenteed to pass.
5549 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5550 if (index < 0 || index >= length) {
5551 codegen_->AddSlowPath(slow_path);
5552 __ jmp(slow_path->GetEntryLabel());
5553 } else {
5554 // Some optimization after BCE may have generated this, and we should not
5555 // generate a bounds check if it is a valid range.
5556 }
5557 return;
5558 }
5559
5560 // We have to reverse the jump condition because the length is the constant.
5561 Register index_reg = index_loc.AsRegister<Register>();
5562 __ cmpl(index_reg, Immediate(length));
5563 codegen_->AddSlowPath(slow_path);
5564 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005565 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005566 Register length = length_loc.AsRegister<Register>();
5567 if (index_loc.IsConstant()) {
5568 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5569 __ cmpl(length, Immediate(value));
5570 } else {
5571 __ cmpl(length, index_loc.AsRegister<Register>());
5572 }
5573 codegen_->AddSlowPath(slow_path);
5574 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005575 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005576}
5577
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005578void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005579 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005580}
5581
5582void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005583 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5584}
5585
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005586void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5587 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5588}
5589
5590void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005591 HBasicBlock* block = instruction->GetBlock();
5592 if (block->GetLoopInformation() != nullptr) {
5593 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5594 // The back edge will generate the suspend check.
5595 return;
5596 }
5597 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5598 // The goto will generate the suspend check.
5599 return;
5600 }
5601 GenerateSuspendCheck(instruction, nullptr);
5602}
5603
5604void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5605 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005606 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005607 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5608 if (slow_path == nullptr) {
5609 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5610 instruction->SetSlowPath(slow_path);
5611 codegen_->AddSlowPath(slow_path);
5612 if (successor != nullptr) {
5613 DCHECK(successor->IsLoopHeader());
5614 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5615 }
5616 } else {
5617 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5618 }
5619
Roland Levillain7c1559a2015-12-15 10:55:36 +00005620 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5621 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005622 if (successor == nullptr) {
5623 __ j(kNotEqual, slow_path->GetEntryLabel());
5624 __ Bind(slow_path->GetReturnLabel());
5625 } else {
5626 __ j(kEqual, codegen_->GetLabelOf(successor));
5627 __ jmp(slow_path->GetEntryLabel());
5628 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005629}
5630
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005631X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5632 return codegen_->GetAssembler();
5633}
5634
Mark Mendell7c8d0092015-01-26 11:21:33 -05005635void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005636 ScratchRegisterScope ensure_scratch(
5637 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5638 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5639 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5640 __ movl(temp_reg, Address(ESP, src + stack_offset));
5641 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005642}
5643
5644void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005645 ScratchRegisterScope ensure_scratch(
5646 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5647 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5648 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5649 __ movl(temp_reg, Address(ESP, src + stack_offset));
5650 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5651 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5652 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005653}
5654
5655void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005656 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005657 Location source = move->GetSource();
5658 Location destination = move->GetDestination();
5659
5660 if (source.IsRegister()) {
5661 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005662 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005663 } else if (destination.IsFpuRegister()) {
5664 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005665 } else {
5666 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005667 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005668 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005669 } else if (source.IsRegisterPair()) {
5670 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5671 // Create stack space for 2 elements.
5672 __ subl(ESP, Immediate(2 * elem_size));
5673 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5674 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5675 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5676 // And remove the temporary stack space we allocated.
5677 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005678 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005679 if (destination.IsRegister()) {
5680 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5681 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005682 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005683 } else if (destination.IsRegisterPair()) {
5684 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5685 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5686 __ psrlq(src_reg, Immediate(32));
5687 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005688 } else if (destination.IsStackSlot()) {
5689 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5690 } else {
5691 DCHECK(destination.IsDoubleStackSlot());
5692 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5693 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005694 } else if (source.IsStackSlot()) {
5695 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005696 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005697 } else if (destination.IsFpuRegister()) {
5698 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005699 } else {
5700 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005701 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5702 }
5703 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005704 if (destination.IsRegisterPair()) {
5705 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5706 __ movl(destination.AsRegisterPairHigh<Register>(),
5707 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5708 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005709 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5710 } else {
5711 DCHECK(destination.IsDoubleStackSlot()) << destination;
5712 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005713 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005714 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005715 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005716 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005717 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005718 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005719 if (value == 0) {
5720 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5721 } else {
5722 __ movl(destination.AsRegister<Register>(), Immediate(value));
5723 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005724 } else {
5725 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005726 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005727 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005728 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005729 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005730 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005731 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005732 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005733 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5734 if (value == 0) {
5735 // Easy handling of 0.0.
5736 __ xorps(dest, dest);
5737 } else {
5738 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005739 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5740 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5741 __ movl(temp, Immediate(value));
5742 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005743 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005744 } else {
5745 DCHECK(destination.IsStackSlot()) << destination;
5746 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5747 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005748 } else if (constant->IsLongConstant()) {
5749 int64_t value = constant->AsLongConstant()->GetValue();
5750 int32_t low_value = Low32Bits(value);
5751 int32_t high_value = High32Bits(value);
5752 Immediate low(low_value);
5753 Immediate high(high_value);
5754 if (destination.IsDoubleStackSlot()) {
5755 __ movl(Address(ESP, destination.GetStackIndex()), low);
5756 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5757 } else {
5758 __ movl(destination.AsRegisterPairLow<Register>(), low);
5759 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5760 }
5761 } else {
5762 DCHECK(constant->IsDoubleConstant());
5763 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005764 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005765 int32_t low_value = Low32Bits(value);
5766 int32_t high_value = High32Bits(value);
5767 Immediate low(low_value);
5768 Immediate high(high_value);
5769 if (destination.IsFpuRegister()) {
5770 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5771 if (value == 0) {
5772 // Easy handling of 0.0.
5773 __ xorpd(dest, dest);
5774 } else {
5775 __ pushl(high);
5776 __ pushl(low);
5777 __ movsd(dest, Address(ESP, 0));
5778 __ addl(ESP, Immediate(8));
5779 }
5780 } else {
5781 DCHECK(destination.IsDoubleStackSlot()) << destination;
5782 __ movl(Address(ESP, destination.GetStackIndex()), low);
5783 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5784 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005785 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005786 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005787 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005788 }
5789}
5790
Mark Mendella5c19ce2015-04-01 12:51:05 -04005791void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005792 Register suggested_scratch = reg == EAX ? EBX : EAX;
5793 ScratchRegisterScope ensure_scratch(
5794 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5795
5796 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5797 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5798 __ movl(Address(ESP, mem + stack_offset), reg);
5799 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005800}
5801
Mark Mendell7c8d0092015-01-26 11:21:33 -05005802void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005803 ScratchRegisterScope ensure_scratch(
5804 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5805
5806 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5807 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5808 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5809 __ movss(Address(ESP, mem + stack_offset), reg);
5810 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005811}
5812
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005813void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005814 ScratchRegisterScope ensure_scratch1(
5815 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005816
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005817 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5818 ScratchRegisterScope ensure_scratch2(
5819 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005820
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005821 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5822 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5823 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5824 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5825 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5826 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005827}
5828
5829void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005830 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005831 Location source = move->GetSource();
5832 Location destination = move->GetDestination();
5833
5834 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005835 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5836 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5837 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5838 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5839 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005840 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005841 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005842 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005843 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005844 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5845 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005846 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5847 // Use XOR Swap algorithm to avoid a temporary.
5848 DCHECK_NE(source.reg(), destination.reg());
5849 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5850 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5851 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5852 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5853 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5854 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5855 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005856 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5857 // Take advantage of the 16 bytes in the XMM register.
5858 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5859 Address stack(ESP, destination.GetStackIndex());
5860 // Load the double into the high doubleword.
5861 __ movhpd(reg, stack);
5862
5863 // Store the low double into the destination.
5864 __ movsd(stack, reg);
5865
5866 // Move the high double to the low double.
5867 __ psrldq(reg, Immediate(8));
5868 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5869 // Take advantage of the 16 bytes in the XMM register.
5870 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5871 Address stack(ESP, source.GetStackIndex());
5872 // Load the double into the high doubleword.
5873 __ movhpd(reg, stack);
5874
5875 // Store the low double into the destination.
5876 __ movsd(stack, reg);
5877
5878 // Move the high double to the low double.
5879 __ psrldq(reg, Immediate(8));
5880 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5881 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5882 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005883 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005884 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005885 }
5886}
5887
5888void ParallelMoveResolverX86::SpillScratch(int reg) {
5889 __ pushl(static_cast<Register>(reg));
5890}
5891
5892void ParallelMoveResolverX86::RestoreScratch(int reg) {
5893 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005894}
5895
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005896void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005897 InvokeRuntimeCallingConvention calling_convention;
5898 CodeGenerator::CreateLoadClassLocationSummary(
5899 cls,
5900 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005901 Location::RegisterLocation(EAX),
5902 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005903}
5904
5905void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005906 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005907 if (cls->NeedsAccessCheck()) {
5908 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5909 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5910 cls,
5911 cls->GetDexPc(),
5912 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005913 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005914 return;
5915 }
5916
Roland Levillain0d5a2812015-11-13 10:07:31 +00005917 Location out_loc = locations->Out();
5918 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005919 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005920
Calin Juravle580b6092015-10-06 17:35:58 +01005921 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005922 DCHECK(!cls->CanCallRuntime());
5923 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005924 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5925 GenerateGcRootFieldLoad(
5926 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005927 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005928 // /* GcRoot<mirror::Class>[] */ out =
5929 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5930 __ movl(out, Address(current_method,
5931 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005932 // /* GcRoot<mirror::Class> */ out = out[type_index]
5933 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005934
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005935 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5936 DCHECK(cls->CanCallRuntime());
5937 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5938 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5939 codegen_->AddSlowPath(slow_path);
5940
5941 if (!cls->IsInDexCache()) {
5942 __ testl(out, out);
5943 __ j(kEqual, slow_path->GetEntryLabel());
5944 }
5945
5946 if (cls->MustGenerateClinitCheck()) {
5947 GenerateClassInitializationCheck(slow_path, out);
5948 } else {
5949 __ Bind(slow_path->GetExitLabel());
5950 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005951 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005952 }
5953}
5954
5955void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5956 LocationSummary* locations =
5957 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5958 locations->SetInAt(0, Location::RequiresRegister());
5959 if (check->HasUses()) {
5960 locations->SetOut(Location::SameAsFirstInput());
5961 }
5962}
5963
5964void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005965 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005966 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005967 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005968 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005969 GenerateClassInitializationCheck(slow_path,
5970 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005971}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005972
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005973void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005974 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005975 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5976 Immediate(mirror::Class::kStatusInitialized));
5977 __ j(kLess, slow_path->GetEntryLabel());
5978 __ Bind(slow_path->GetExitLabel());
5979 // No need for memory fence, thanks to the X86 memory model.
5980}
5981
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005982void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005983 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5984 ? LocationSummary::kCallOnSlowPath
5985 : LocationSummary::kNoCall;
5986 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005987 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005988 locations->SetOut(Location::RequiresRegister());
5989}
5990
5991void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005992 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005993 Location out_loc = locations->Out();
5994 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005995 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005996
Roland Levillain7c1559a2015-12-15 10:55:36 +00005997 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5998 GenerateGcRootFieldLoad(
5999 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006000 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08006001 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00006002 // /* GcRoot<mirror::String> */ out = out[string_index]
6003 GenerateGcRootFieldLoad(
6004 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006005
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006006 if (!load->IsInDexCache()) {
6007 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6008 codegen_->AddSlowPath(slow_path);
6009 __ testl(out, out);
6010 __ j(kEqual, slow_path->GetEntryLabel());
6011 __ Bind(slow_path->GetExitLabel());
6012 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006013}
6014
David Brazdilcb1c0552015-08-04 16:22:25 +01006015static Address GetExceptionTlsAddress() {
6016 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
6017}
6018
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006019void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6020 LocationSummary* locations =
6021 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6022 locations->SetOut(Location::RequiresRegister());
6023}
6024
6025void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006026 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6027}
6028
6029void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6030 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6031}
6032
6033void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6034 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006035}
6036
6037void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6038 LocationSummary* locations =
6039 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6040 InvokeRuntimeCallingConvention calling_convention;
6041 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6042}
6043
6044void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006045 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6046 instruction,
6047 instruction->GetDexPc(),
6048 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006049 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006050}
6051
Roland Levillain7c1559a2015-12-15 10:55:36 +00006052static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6053 return kEmitCompilerReadBarrier &&
6054 (kUseBakerReadBarrier ||
6055 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6056 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6057 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6058}
6059
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006060void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006061 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006062 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6063 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006064 case TypeCheckKind::kExactCheck:
6065 case TypeCheckKind::kAbstractClassCheck:
6066 case TypeCheckKind::kClassHierarchyCheck:
6067 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006068 call_kind =
6069 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006070 break;
6071 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006072 case TypeCheckKind::kUnresolvedCheck:
6073 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006074 call_kind = LocationSummary::kCallOnSlowPath;
6075 break;
6076 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006077
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006078 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006079 locations->SetInAt(0, Location::RequiresRegister());
6080 locations->SetInAt(1, Location::Any());
6081 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6082 locations->SetOut(Location::RequiresRegister());
6083 // When read barriers are enabled, we need a temporary register for
6084 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006085 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006086 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006087 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006088}
6089
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006090void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006091 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006092 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006093 Location obj_loc = locations->InAt(0);
6094 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006095 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006096 Location out_loc = locations->Out();
6097 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006098 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006099 locations->GetTemp(0) :
6100 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006101 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006102 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6103 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6104 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006105 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006106 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006107
6108 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006109 // Avoid null check if we know obj is not null.
6110 if (instruction->MustDoNullCheck()) {
6111 __ testl(obj, obj);
6112 __ j(kEqual, &zero);
6113 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006114
Roland Levillain0d5a2812015-11-13 10:07:31 +00006115 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006116 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006117
Roland Levillain7c1559a2015-12-15 10:55:36 +00006118 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006119 case TypeCheckKind::kExactCheck: {
6120 if (cls.IsRegister()) {
6121 __ cmpl(out, cls.AsRegister<Register>());
6122 } else {
6123 DCHECK(cls.IsStackSlot()) << cls;
6124 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6125 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006126
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006127 // Classes must be equal for the instanceof to succeed.
6128 __ j(kNotEqual, &zero);
6129 __ movl(out, Immediate(1));
6130 __ jmp(&done);
6131 break;
6132 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006133
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006134 case TypeCheckKind::kAbstractClassCheck: {
6135 // If the class is abstract, we eagerly fetch the super class of the
6136 // object to avoid doing a comparison we know will fail.
6137 NearLabel loop;
6138 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006139 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006140 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006141 __ testl(out, out);
6142 // If `out` is null, we use it for the result, and jump to `done`.
6143 __ j(kEqual, &done);
6144 if (cls.IsRegister()) {
6145 __ cmpl(out, cls.AsRegister<Register>());
6146 } else {
6147 DCHECK(cls.IsStackSlot()) << cls;
6148 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6149 }
6150 __ j(kNotEqual, &loop);
6151 __ movl(out, Immediate(1));
6152 if (zero.IsLinked()) {
6153 __ jmp(&done);
6154 }
6155 break;
6156 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006157
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006158 case TypeCheckKind::kClassHierarchyCheck: {
6159 // Walk over the class hierarchy to find a match.
6160 NearLabel loop, success;
6161 __ Bind(&loop);
6162 if (cls.IsRegister()) {
6163 __ cmpl(out, cls.AsRegister<Register>());
6164 } else {
6165 DCHECK(cls.IsStackSlot()) << cls;
6166 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6167 }
6168 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006169 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006170 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006171 __ testl(out, out);
6172 __ j(kNotEqual, &loop);
6173 // If `out` is null, we use it for the result, and jump to `done`.
6174 __ jmp(&done);
6175 __ Bind(&success);
6176 __ movl(out, Immediate(1));
6177 if (zero.IsLinked()) {
6178 __ jmp(&done);
6179 }
6180 break;
6181 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006182
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006183 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006184 // Do an exact check.
6185 NearLabel exact_check;
6186 if (cls.IsRegister()) {
6187 __ cmpl(out, cls.AsRegister<Register>());
6188 } else {
6189 DCHECK(cls.IsStackSlot()) << cls;
6190 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6191 }
6192 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006193 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006194 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006195 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006196 __ testl(out, out);
6197 // If `out` is null, we use it for the result, and jump to `done`.
6198 __ j(kEqual, &done);
6199 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6200 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006201 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006202 __ movl(out, Immediate(1));
6203 __ jmp(&done);
6204 break;
6205 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006206
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006207 case TypeCheckKind::kArrayCheck: {
6208 if (cls.IsRegister()) {
6209 __ cmpl(out, cls.AsRegister<Register>());
6210 } else {
6211 DCHECK(cls.IsStackSlot()) << cls;
6212 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6213 }
6214 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006215 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6216 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006217 codegen_->AddSlowPath(slow_path);
6218 __ j(kNotEqual, slow_path->GetEntryLabel());
6219 __ movl(out, Immediate(1));
6220 if (zero.IsLinked()) {
6221 __ jmp(&done);
6222 }
6223 break;
6224 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006225
Calin Juravle98893e12015-10-02 21:05:03 +01006226 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006227 case TypeCheckKind::kInterfaceCheck: {
6228 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006229 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006230 // cases.
6231 //
6232 // We cannot directly call the InstanceofNonTrivial runtime
6233 // entry point without resorting to a type checking slow path
6234 // here (i.e. by calling InvokeRuntime directly), as it would
6235 // require to assign fixed registers for the inputs of this
6236 // HInstanceOf instruction (following the runtime calling
6237 // convention), which might be cluttered by the potential first
6238 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006239 //
6240 // TODO: Introduce a new runtime entry point taking the object
6241 // to test (instead of its class) as argument, and let it deal
6242 // with the read barrier issues. This will let us refactor this
6243 // case of the `switch` code as it was previously (with a direct
6244 // call to the runtime not using a type checking slow path).
6245 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006246 DCHECK(locations->OnlyCallsOnSlowPath());
6247 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6248 /* is_fatal */ false);
6249 codegen_->AddSlowPath(slow_path);
6250 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006251 if (zero.IsLinked()) {
6252 __ jmp(&done);
6253 }
6254 break;
6255 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006256 }
6257
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006258 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006259 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006260 __ xorl(out, out);
6261 }
6262
6263 if (done.IsLinked()) {
6264 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006265 }
6266
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006267 if (slow_path != nullptr) {
6268 __ Bind(slow_path->GetExitLabel());
6269 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006270}
6271
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006272void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006273 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6274 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006275 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6276 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006277 case TypeCheckKind::kExactCheck:
6278 case TypeCheckKind::kAbstractClassCheck:
6279 case TypeCheckKind::kClassHierarchyCheck:
6280 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006281 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6282 LocationSummary::kCallOnSlowPath :
6283 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006284 break;
6285 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006286 case TypeCheckKind::kUnresolvedCheck:
6287 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006288 call_kind = LocationSummary::kCallOnSlowPath;
6289 break;
6290 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006291 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6292 locations->SetInAt(0, Location::RequiresRegister());
6293 locations->SetInAt(1, Location::Any());
6294 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6295 locations->AddTemp(Location::RequiresRegister());
6296 // When read barriers are enabled, we need an additional temporary
6297 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006298 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006299 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006300 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006301}
6302
6303void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006304 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006305 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006306 Location obj_loc = locations->InAt(0);
6307 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006308 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006309 Location temp_loc = locations->GetTemp(0);
6310 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006311 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006312 locations->GetTemp(1) :
6313 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006314 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6315 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6316 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6317 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006318
Roland Levillain0d5a2812015-11-13 10:07:31 +00006319 bool is_type_check_slow_path_fatal =
6320 (type_check_kind == TypeCheckKind::kExactCheck ||
6321 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6322 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6323 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6324 !instruction->CanThrowIntoCatchBlock();
6325 SlowPathCode* type_check_slow_path =
6326 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6327 is_type_check_slow_path_fatal);
6328 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006329
Roland Levillain0d5a2812015-11-13 10:07:31 +00006330 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006331 // Avoid null check if we know obj is not null.
6332 if (instruction->MustDoNullCheck()) {
6333 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006334 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006335 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006336
Roland Levillain0d5a2812015-11-13 10:07:31 +00006337 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006338 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006339
Roland Levillain0d5a2812015-11-13 10:07:31 +00006340 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006341 case TypeCheckKind::kExactCheck:
6342 case TypeCheckKind::kArrayCheck: {
6343 if (cls.IsRegister()) {
6344 __ cmpl(temp, cls.AsRegister<Register>());
6345 } else {
6346 DCHECK(cls.IsStackSlot()) << cls;
6347 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6348 }
6349 // Jump to slow path for throwing the exception or doing a
6350 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006351 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006352 break;
6353 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006354
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006355 case TypeCheckKind::kAbstractClassCheck: {
6356 // If the class is abstract, we eagerly fetch the super class of the
6357 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006358 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006359 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006360 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006361 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006362
6363 // If the class reference currently in `temp` is not null, jump
6364 // to the `compare_classes` label to compare it with the checked
6365 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006366 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006367 __ j(kNotEqual, &compare_classes);
6368 // Otherwise, jump to the slow path to throw the exception.
6369 //
6370 // But before, move back the object's class into `temp` before
6371 // going into the slow path, as it has been overwritten in the
6372 // meantime.
6373 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006374 GenerateReferenceLoadTwoRegisters(
6375 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006376 __ jmp(type_check_slow_path->GetEntryLabel());
6377
6378 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006379 if (cls.IsRegister()) {
6380 __ cmpl(temp, cls.AsRegister<Register>());
6381 } else {
6382 DCHECK(cls.IsStackSlot()) << cls;
6383 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6384 }
6385 __ j(kNotEqual, &loop);
6386 break;
6387 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006388
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006389 case TypeCheckKind::kClassHierarchyCheck: {
6390 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006391 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006392 __ Bind(&loop);
6393 if (cls.IsRegister()) {
6394 __ cmpl(temp, cls.AsRegister<Register>());
6395 } else {
6396 DCHECK(cls.IsStackSlot()) << cls;
6397 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6398 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006399 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006400
Roland Levillain0d5a2812015-11-13 10:07:31 +00006401 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006402 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006403
6404 // If the class reference currently in `temp` is not null, jump
6405 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006406 __ testl(temp, temp);
6407 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006408 // Otherwise, jump to the slow path to throw the exception.
6409 //
6410 // But before, move back the object's class into `temp` before
6411 // going into the slow path, as it has been overwritten in the
6412 // meantime.
6413 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006414 GenerateReferenceLoadTwoRegisters(
6415 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006416 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006417 break;
6418 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006419
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006420 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006421 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006422 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006423 if (cls.IsRegister()) {
6424 __ cmpl(temp, cls.AsRegister<Register>());
6425 } else {
6426 DCHECK(cls.IsStackSlot()) << cls;
6427 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6428 }
6429 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006430
6431 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006432 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006433 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006434
6435 // If the component type is not null (i.e. the object is indeed
6436 // an array), jump to label `check_non_primitive_component_type`
6437 // to further check that this component type is not a primitive
6438 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006439 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006440 __ j(kNotEqual, &check_non_primitive_component_type);
6441 // Otherwise, jump to the slow path to throw the exception.
6442 //
6443 // But before, move back the object's class into `temp` before
6444 // going into the slow path, as it has been overwritten in the
6445 // meantime.
6446 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006447 GenerateReferenceLoadTwoRegisters(
6448 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006449 __ jmp(type_check_slow_path->GetEntryLabel());
6450
6451 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006452 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006453 __ j(kEqual, &done);
6454 // Same comment as above regarding `temp` and the slow path.
6455 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006456 GenerateReferenceLoadTwoRegisters(
6457 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006458 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006459 break;
6460 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006461
Calin Juravle98893e12015-10-02 21:05:03 +01006462 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006463 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006464 // We always go into the type check slow path for the unresolved
6465 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006466 //
6467 // We cannot directly call the CheckCast runtime entry point
6468 // without resorting to a type checking slow path here (i.e. by
6469 // calling InvokeRuntime directly), as it would require to
6470 // assign fixed registers for the inputs of this HInstanceOf
6471 // instruction (following the runtime calling convention), which
6472 // might be cluttered by the potential first read barrier
6473 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006474 //
6475 // TODO: Introduce a new runtime entry point taking the object
6476 // to test (instead of its class) as argument, and let it deal
6477 // with the read barrier issues. This will let us refactor this
6478 // case of the `switch` code as it was previously (with a direct
6479 // call to the runtime not using a type checking slow path).
6480 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006481 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006482 break;
6483 }
6484 __ Bind(&done);
6485
Roland Levillain0d5a2812015-11-13 10:07:31 +00006486 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006487}
6488
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006489void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6490 LocationSummary* locations =
6491 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6492 InvokeRuntimeCallingConvention calling_convention;
6493 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6494}
6495
6496void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006497 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6498 : QUICK_ENTRY_POINT(pUnlockObject),
6499 instruction,
6500 instruction->GetDexPc(),
6501 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006502 if (instruction->IsEnter()) {
6503 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6504 } else {
6505 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6506 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006507}
6508
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006509void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6510void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6511void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6512
6513void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6514 LocationSummary* locations =
6515 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6516 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6517 || instruction->GetResultType() == Primitive::kPrimLong);
6518 locations->SetInAt(0, Location::RequiresRegister());
6519 locations->SetInAt(1, Location::Any());
6520 locations->SetOut(Location::SameAsFirstInput());
6521}
6522
6523void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6524 HandleBitwiseOperation(instruction);
6525}
6526
6527void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6528 HandleBitwiseOperation(instruction);
6529}
6530
6531void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6532 HandleBitwiseOperation(instruction);
6533}
6534
6535void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6536 LocationSummary* locations = instruction->GetLocations();
6537 Location first = locations->InAt(0);
6538 Location second = locations->InAt(1);
6539 DCHECK(first.Equals(locations->Out()));
6540
6541 if (instruction->GetResultType() == Primitive::kPrimInt) {
6542 if (second.IsRegister()) {
6543 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006544 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006545 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006546 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006547 } else {
6548 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006549 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006550 }
6551 } else if (second.IsConstant()) {
6552 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006553 __ andl(first.AsRegister<Register>(),
6554 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006555 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006556 __ orl(first.AsRegister<Register>(),
6557 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006558 } else {
6559 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006560 __ xorl(first.AsRegister<Register>(),
6561 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006562 }
6563 } else {
6564 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006565 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006566 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006567 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006568 } else {
6569 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006570 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006571 }
6572 }
6573 } else {
6574 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6575 if (second.IsRegisterPair()) {
6576 if (instruction->IsAnd()) {
6577 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6578 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6579 } else if (instruction->IsOr()) {
6580 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6581 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6582 } else {
6583 DCHECK(instruction->IsXor());
6584 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6585 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6586 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006587 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006588 if (instruction->IsAnd()) {
6589 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6590 __ andl(first.AsRegisterPairHigh<Register>(),
6591 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6592 } else if (instruction->IsOr()) {
6593 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6594 __ orl(first.AsRegisterPairHigh<Register>(),
6595 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6596 } else {
6597 DCHECK(instruction->IsXor());
6598 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6599 __ xorl(first.AsRegisterPairHigh<Register>(),
6600 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6601 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006602 } else {
6603 DCHECK(second.IsConstant()) << second;
6604 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006605 int32_t low_value = Low32Bits(value);
6606 int32_t high_value = High32Bits(value);
6607 Immediate low(low_value);
6608 Immediate high(high_value);
6609 Register first_low = first.AsRegisterPairLow<Register>();
6610 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006611 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006612 if (low_value == 0) {
6613 __ xorl(first_low, first_low);
6614 } else if (low_value != -1) {
6615 __ andl(first_low, low);
6616 }
6617 if (high_value == 0) {
6618 __ xorl(first_high, first_high);
6619 } else if (high_value != -1) {
6620 __ andl(first_high, high);
6621 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006622 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006623 if (low_value != 0) {
6624 __ orl(first_low, low);
6625 }
6626 if (high_value != 0) {
6627 __ orl(first_high, high);
6628 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006629 } else {
6630 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006631 if (low_value != 0) {
6632 __ xorl(first_low, low);
6633 }
6634 if (high_value != 0) {
6635 __ xorl(first_high, high);
6636 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006637 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006638 }
6639 }
6640}
6641
Roland Levillain7c1559a2015-12-15 10:55:36 +00006642void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6643 Location out,
6644 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006645 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006646 Register out_reg = out.AsRegister<Register>();
6647 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006648 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006649 if (kUseBakerReadBarrier) {
6650 // Load with fast path based Baker's read barrier.
6651 // /* HeapReference<Object> */ out = *(out + offset)
6652 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006653 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006654 } else {
6655 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006656 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006657 // in the following move operation, as we will need it for the
6658 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006659 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006660 // /* HeapReference<Object> */ out = *(out + offset)
6661 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006662 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006663 }
6664 } else {
6665 // Plain load with no read barrier.
6666 // /* HeapReference<Object> */ out = *(out + offset)
6667 __ movl(out_reg, Address(out_reg, offset));
6668 __ MaybeUnpoisonHeapReference(out_reg);
6669 }
6670}
6671
6672void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6673 Location out,
6674 Location obj,
6675 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006676 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006677 Register out_reg = out.AsRegister<Register>();
6678 Register obj_reg = obj.AsRegister<Register>();
6679 if (kEmitCompilerReadBarrier) {
6680 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006681 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006682 // Load with fast path based Baker's read barrier.
6683 // /* HeapReference<Object> */ out = *(obj + offset)
6684 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006685 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006686 } else {
6687 // Load with slow path based read barrier.
6688 // /* HeapReference<Object> */ out = *(obj + offset)
6689 __ movl(out_reg, Address(obj_reg, offset));
6690 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6691 }
6692 } else {
6693 // Plain load with no read barrier.
6694 // /* HeapReference<Object> */ out = *(obj + offset)
6695 __ movl(out_reg, Address(obj_reg, offset));
6696 __ MaybeUnpoisonHeapReference(out_reg);
6697 }
6698}
6699
6700void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6701 Location root,
6702 Register obj,
6703 uint32_t offset) {
6704 Register root_reg = root.AsRegister<Register>();
6705 if (kEmitCompilerReadBarrier) {
6706 if (kUseBakerReadBarrier) {
6707 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6708 // Baker's read barrier are used:
6709 //
6710 // root = obj.field;
6711 // if (Thread::Current()->GetIsGcMarking()) {
6712 // root = ReadBarrier::Mark(root)
6713 // }
6714
6715 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6716 __ movl(root_reg, Address(obj, offset));
6717 static_assert(
6718 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6719 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6720 "have different sizes.");
6721 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6722 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6723 "have different sizes.");
6724
6725 // Slow path used to mark the GC root `root`.
6726 SlowPathCode* slow_path =
6727 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6728 codegen_->AddSlowPath(slow_path);
6729
6730 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6731 Immediate(0));
6732 __ j(kNotEqual, slow_path->GetEntryLabel());
6733 __ Bind(slow_path->GetExitLabel());
6734 } else {
6735 // GC root loaded through a slow path for read barriers other
6736 // than Baker's.
6737 // /* GcRoot<mirror::Object>* */ root = obj + offset
6738 __ leal(root_reg, Address(obj, offset));
6739 // /* mirror::Object* */ root = root->Read()
6740 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6741 }
6742 } else {
6743 // Plain GC root load with no read barrier.
6744 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6745 __ movl(root_reg, Address(obj, offset));
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006746 // Note that GC roots are not affected by heap poisoning, thus we
6747 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006748 }
6749}
6750
6751void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6752 Location ref,
6753 Register obj,
6754 uint32_t offset,
6755 Location temp,
6756 bool needs_null_check) {
6757 DCHECK(kEmitCompilerReadBarrier);
6758 DCHECK(kUseBakerReadBarrier);
6759
6760 // /* HeapReference<Object> */ ref = *(obj + offset)
6761 Address src(obj, offset);
6762 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6763}
6764
6765void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6766 Location ref,
6767 Register obj,
6768 uint32_t data_offset,
6769 Location index,
6770 Location temp,
6771 bool needs_null_check) {
6772 DCHECK(kEmitCompilerReadBarrier);
6773 DCHECK(kUseBakerReadBarrier);
6774
6775 // /* HeapReference<Object> */ ref =
6776 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6777 Address src = index.IsConstant() ?
6778 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6779 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6780 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6781}
6782
6783void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6784 Location ref,
6785 Register obj,
6786 const Address& src,
6787 Location temp,
6788 bool needs_null_check) {
6789 DCHECK(kEmitCompilerReadBarrier);
6790 DCHECK(kUseBakerReadBarrier);
6791
6792 // In slow path based read barriers, the read barrier call is
6793 // inserted after the original load. However, in fast path based
6794 // Baker's read barriers, we need to perform the load of
6795 // mirror::Object::monitor_ *before* the original reference load.
6796 // This load-load ordering is required by the read barrier.
6797 // The fast path/slow path (for Baker's algorithm) should look like:
6798 //
6799 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6800 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6801 // HeapReference<Object> ref = *src; // Original reference load.
6802 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6803 // if (is_gray) {
6804 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6805 // }
6806 //
6807 // Note: the original implementation in ReadBarrier::Barrier is
6808 // slightly more complex as:
6809 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006810 // the high-bits of rb_state, which are expected to be all zeroes
6811 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6812 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006813 // - it performs additional checks that we do not do here for
6814 // performance reasons.
6815
6816 Register ref_reg = ref.AsRegister<Register>();
6817 Register temp_reg = temp.AsRegister<Register>();
6818 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6819
6820 // /* int32_t */ monitor = obj->monitor_
6821 __ movl(temp_reg, Address(obj, monitor_offset));
6822 if (needs_null_check) {
6823 MaybeRecordImplicitNullCheck(instruction);
6824 }
6825 // /* LockWord */ lock_word = LockWord(monitor)
6826 static_assert(sizeof(LockWord) == sizeof(int32_t),
6827 "art::LockWord and int32_t have different sizes.");
6828 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6829 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6830 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6831 static_assert(
6832 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6833 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6834
6835 // Load fence to prevent load-load reordering.
6836 // Note that this is a no-op, thanks to the x86 memory model.
6837 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6838
6839 // The actual reference load.
6840 // /* HeapReference<Object> */ ref = *src
6841 __ movl(ref_reg, src);
6842
6843 // Object* ref = ref_addr->AsMirrorPtr()
6844 __ MaybeUnpoisonHeapReference(ref_reg);
6845
6846 // Slow path used to mark the object `ref` when it is gray.
6847 SlowPathCode* slow_path =
6848 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6849 AddSlowPath(slow_path);
6850
6851 // if (rb_state == ReadBarrier::gray_ptr_)
6852 // ref = ReadBarrier::Mark(ref);
6853 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6854 __ j(kEqual, slow_path->GetEntryLabel());
6855 __ Bind(slow_path->GetExitLabel());
6856}
6857
6858void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6859 Location out,
6860 Location ref,
6861 Location obj,
6862 uint32_t offset,
6863 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006864 DCHECK(kEmitCompilerReadBarrier);
6865
Roland Levillain7c1559a2015-12-15 10:55:36 +00006866 // Insert a slow path based read barrier *after* the reference load.
6867 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006868 // If heap poisoning is enabled, the unpoisoning of the loaded
6869 // reference will be carried out by the runtime within the slow
6870 // path.
6871 //
6872 // Note that `ref` currently does not get unpoisoned (when heap
6873 // poisoning is enabled), which is alright as the `ref` argument is
6874 // not used by the artReadBarrierSlow entry point.
6875 //
6876 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6877 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6878 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6879 AddSlowPath(slow_path);
6880
Roland Levillain0d5a2812015-11-13 10:07:31 +00006881 __ jmp(slow_path->GetEntryLabel());
6882 __ Bind(slow_path->GetExitLabel());
6883}
6884
Roland Levillain7c1559a2015-12-15 10:55:36 +00006885void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6886 Location out,
6887 Location ref,
6888 Location obj,
6889 uint32_t offset,
6890 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006891 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006892 // Baker's read barriers shall be handled by the fast path
6893 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6894 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006895 // If heap poisoning is enabled, unpoisoning will be taken care of
6896 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006897 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006898 } else if (kPoisonHeapReferences) {
6899 __ UnpoisonHeapReference(out.AsRegister<Register>());
6900 }
6901}
6902
Roland Levillain7c1559a2015-12-15 10:55:36 +00006903void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6904 Location out,
6905 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006906 DCHECK(kEmitCompilerReadBarrier);
6907
Roland Levillain7c1559a2015-12-15 10:55:36 +00006908 // Insert a slow path based read barrier *after* the GC root load.
6909 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006910 // Note that GC roots are not affected by heap poisoning, so we do
6911 // not need to do anything special for this here.
6912 SlowPathCode* slow_path =
6913 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6914 AddSlowPath(slow_path);
6915
Roland Levillain0d5a2812015-11-13 10:07:31 +00006916 __ jmp(slow_path->GetEntryLabel());
6917 __ Bind(slow_path->GetExitLabel());
6918}
6919
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006920void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006921 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006922 LOG(FATAL) << "Unreachable";
6923}
6924
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006925void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006926 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006927 LOG(FATAL) << "Unreachable";
6928}
6929
Mark Mendellfe57faa2015-09-18 09:26:15 -04006930// Simple implementation of packed switch - generate cascaded compare/jumps.
6931void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6932 LocationSummary* locations =
6933 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6934 locations->SetInAt(0, Location::RequiresRegister());
6935}
6936
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006937void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
6938 int32_t lower_bound,
6939 uint32_t num_entries,
6940 HBasicBlock* switch_block,
6941 HBasicBlock* default_block) {
6942 // Figure out the correct compare values and jump conditions.
6943 // Handle the first compare/branch as a special case because it might
6944 // jump to the default case.
6945 DCHECK_GT(num_entries, 2u);
6946 Condition first_condition;
6947 uint32_t index;
6948 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6949 if (lower_bound != 0) {
6950 first_condition = kLess;
6951 __ cmpl(value_reg, Immediate(lower_bound));
6952 __ j(first_condition, codegen_->GetLabelOf(default_block));
6953 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006954
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006955 index = 1;
6956 } else {
6957 // Handle all the compare/jumps below.
6958 first_condition = kBelow;
6959 index = 0;
6960 }
6961
6962 // Handle the rest of the compare/jumps.
6963 for (; index + 1 < num_entries; index += 2) {
6964 int32_t compare_to_value = lower_bound + index + 1;
6965 __ cmpl(value_reg, Immediate(compare_to_value));
6966 // Jump to successors[index] if value < case_value[index].
6967 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6968 // Jump to successors[index + 1] if value == case_value[index + 1].
6969 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6970 }
6971
6972 if (index != num_entries) {
6973 // There are an odd number of entries. Handle the last one.
6974 DCHECK_EQ(index + 1, num_entries);
6975 __ cmpl(value_reg, Immediate(lower_bound + index));
6976 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006977 }
6978
6979 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006980 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
6981 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006982 }
6983}
6984
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006985void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6986 int32_t lower_bound = switch_instr->GetStartValue();
6987 uint32_t num_entries = switch_instr->GetNumEntries();
6988 LocationSummary* locations = switch_instr->GetLocations();
6989 Register value_reg = locations->InAt(0).AsRegister<Register>();
6990
6991 GenPackedSwitchWithCompares(value_reg,
6992 lower_bound,
6993 num_entries,
6994 switch_instr->GetBlock(),
6995 switch_instr->GetDefaultBlock());
6996}
6997
Mark Mendell805b3b52015-09-18 14:10:29 -04006998void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6999 LocationSummary* locations =
7000 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7001 locations->SetInAt(0, Location::RequiresRegister());
7002
7003 // Constant area pointer.
7004 locations->SetInAt(1, Location::RequiresRegister());
7005
7006 // And the temporary we need.
7007 locations->AddTemp(Location::RequiresRegister());
7008}
7009
7010void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7011 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007012 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007013 LocationSummary* locations = switch_instr->GetLocations();
7014 Register value_reg = locations->InAt(0).AsRegister<Register>();
7015 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7016
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007017 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7018 GenPackedSwitchWithCompares(value_reg,
7019 lower_bound,
7020 num_entries,
7021 switch_instr->GetBlock(),
7022 default_block);
7023 return;
7024 }
7025
Mark Mendell805b3b52015-09-18 14:10:29 -04007026 // Optimizing has a jump area.
7027 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7028 Register constant_area = locations->InAt(1).AsRegister<Register>();
7029
7030 // Remove the bias, if needed.
7031 if (lower_bound != 0) {
7032 __ leal(temp_reg, Address(value_reg, -lower_bound));
7033 value_reg = temp_reg;
7034 }
7035
7036 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007037 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007038 __ cmpl(value_reg, Immediate(num_entries - 1));
7039 __ j(kAbove, codegen_->GetLabelOf(default_block));
7040
7041 // We are in the range of the table.
7042 // Load (target-constant_area) from the jump table, indexing by the value.
7043 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7044
7045 // Compute the actual target address by adding in constant_area.
7046 __ addl(temp_reg, constant_area);
7047
7048 // And jump.
7049 __ jmp(temp_reg);
7050}
7051
Mark Mendell0616ae02015-04-17 12:49:27 -04007052void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7053 HX86ComputeBaseMethodAddress* insn) {
7054 LocationSummary* locations =
7055 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7056 locations->SetOut(Location::RequiresRegister());
7057}
7058
7059void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7060 HX86ComputeBaseMethodAddress* insn) {
7061 LocationSummary* locations = insn->GetLocations();
7062 Register reg = locations->Out().AsRegister<Register>();
7063
7064 // Generate call to next instruction.
7065 Label next_instruction;
7066 __ call(&next_instruction);
7067 __ Bind(&next_instruction);
7068
7069 // Remember this offset for later use with constant area.
7070 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7071
7072 // Grab the return address off the stack.
7073 __ popl(reg);
7074}
7075
7076void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7077 HX86LoadFromConstantTable* insn) {
7078 LocationSummary* locations =
7079 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7080
7081 locations->SetInAt(0, Location::RequiresRegister());
7082 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7083
7084 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007085 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007086 return;
7087 }
7088
7089 switch (insn->GetType()) {
7090 case Primitive::kPrimFloat:
7091 case Primitive::kPrimDouble:
7092 locations->SetOut(Location::RequiresFpuRegister());
7093 break;
7094
7095 case Primitive::kPrimInt:
7096 locations->SetOut(Location::RequiresRegister());
7097 break;
7098
7099 default:
7100 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7101 }
7102}
7103
7104void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007105 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007106 return;
7107 }
7108
7109 LocationSummary* locations = insn->GetLocations();
7110 Location out = locations->Out();
7111 Register const_area = locations->InAt(0).AsRegister<Register>();
7112 HConstant *value = insn->GetConstant();
7113
7114 switch (insn->GetType()) {
7115 case Primitive::kPrimFloat:
7116 __ movss(out.AsFpuRegister<XmmRegister>(),
7117 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7118 break;
7119
7120 case Primitive::kPrimDouble:
7121 __ movsd(out.AsFpuRegister<XmmRegister>(),
7122 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7123 break;
7124
7125 case Primitive::kPrimInt:
7126 __ movl(out.AsRegister<Register>(),
7127 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7128 break;
7129
7130 default:
7131 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7132 }
7133}
7134
Mark Mendell0616ae02015-04-17 12:49:27 -04007135/**
7136 * Class to handle late fixup of offsets into constant area.
7137 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007138class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007139 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007140 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7141 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7142
7143 protected:
7144 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7145
7146 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007147
7148 private:
7149 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7150 // Patch the correct offset for the instruction. The place to patch is the
7151 // last 4 bytes of the instruction.
7152 // The value to patch is the distance from the offset in the constant area
7153 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007154 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7155 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007156
7157 // Patch in the right value.
7158 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7159 }
7160
Mark Mendell0616ae02015-04-17 12:49:27 -04007161 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007162 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007163};
7164
Mark Mendell805b3b52015-09-18 14:10:29 -04007165/**
7166 * Class to handle late fixup of offsets to a jump table that will be created in the
7167 * constant area.
7168 */
7169class JumpTableRIPFixup : public RIPFixup {
7170 public:
7171 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7172 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7173
7174 void CreateJumpTable() {
7175 X86Assembler* assembler = codegen_->GetAssembler();
7176
7177 // Ensure that the reference to the jump table has the correct offset.
7178 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7179 SetOffset(offset_in_constant_table);
7180
7181 // The label values in the jump table are computed relative to the
7182 // instruction addressing the constant area.
7183 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7184
7185 // Populate the jump table with the correct values for the jump table.
7186 int32_t num_entries = switch_instr_->GetNumEntries();
7187 HBasicBlock* block = switch_instr_->GetBlock();
7188 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7189 // The value that we want is the target offset - the position of the table.
7190 for (int32_t i = 0; i < num_entries; i++) {
7191 HBasicBlock* b = successors[i];
7192 Label* l = codegen_->GetLabelOf(b);
7193 DCHECK(l->IsBound());
7194 int32_t offset_to_block = l->Position() - relative_offset;
7195 assembler->AppendInt32(offset_to_block);
7196 }
7197 }
7198
7199 private:
7200 const HX86PackedSwitch* switch_instr_;
7201};
7202
7203void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7204 // Generate the constant area if needed.
7205 X86Assembler* assembler = GetAssembler();
7206 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7207 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7208 // byte values.
7209 assembler->Align(4, 0);
7210 constant_area_start_ = assembler->CodeSize();
7211
7212 // Populate any jump tables.
7213 for (auto jump_table : fixups_to_jump_tables_) {
7214 jump_table->CreateJumpTable();
7215 }
7216
7217 // And now add the constant area to the generated code.
7218 assembler->AddConstantArea();
7219 }
7220
7221 // And finish up.
7222 CodeGenerator::Finalize(allocator);
7223}
7224
Mark Mendell0616ae02015-04-17 12:49:27 -04007225Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7226 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7227 return Address(reg, kDummy32BitOffset, fixup);
7228}
7229
7230Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7231 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7232 return Address(reg, kDummy32BitOffset, fixup);
7233}
7234
7235Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7236 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7237 return Address(reg, kDummy32BitOffset, fixup);
7238}
7239
7240Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7241 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7242 return Address(reg, kDummy32BitOffset, fixup);
7243}
7244
Aart Bika19616e2016-02-01 18:57:58 -08007245void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7246 if (value == 0) {
7247 __ xorl(dest, dest);
7248 } else {
7249 __ movl(dest, Immediate(value));
7250 }
7251}
7252
7253void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7254 if (value == 0) {
7255 __ testl(dest, dest);
7256 } else {
7257 __ cmpl(dest, Immediate(value));
7258 }
7259}
7260
Mark Mendell805b3b52015-09-18 14:10:29 -04007261Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7262 Register reg,
7263 Register value) {
7264 // Create a fixup to be used to create and address the jump table.
7265 JumpTableRIPFixup* table_fixup =
7266 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7267
7268 // We have to populate the jump tables.
7269 fixups_to_jump_tables_.push_back(table_fixup);
7270
7271 // We want a scaled address, as we are extracting the correct offset from the table.
7272 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7273}
7274
Andreas Gampe85b62f22015-09-09 13:15:38 -07007275// TODO: target as memory.
7276void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7277 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007278 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007279 return;
7280 }
7281
7282 DCHECK_NE(type, Primitive::kPrimVoid);
7283
7284 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7285 if (target.Equals(return_loc)) {
7286 return;
7287 }
7288
7289 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7290 // with the else branch.
7291 if (type == Primitive::kPrimLong) {
7292 HParallelMove parallel_move(GetGraph()->GetArena());
7293 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7294 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7295 GetMoveResolver()->EmitNativeCode(&parallel_move);
7296 } else {
7297 // Let the parallel move resolver take care of all of this.
7298 HParallelMove parallel_move(GetGraph()->GetArena());
7299 parallel_move.AddMove(return_loc, target, type, nullptr);
7300 GetMoveResolver()->EmitNativeCode(&parallel_move);
7301 }
7302}
7303
Roland Levillain4d027112015-07-01 15:41:14 +01007304#undef __
7305
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007306} // namespace x86
7307} // namespace art