blob: da27928ef21c12a907b5093791f2a5828557fbb1 [file] [log] [blame]
Nicolas Geoffray76716a62014-05-23 10:14:19 +01001/*
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#ifndef ART_COMPILER_OPTIMIZING_LOCATIONS_H_
18#define ART_COMPILER_OPTIMIZING_LOCATIONS_H_
19
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Nicolas Geoffray76716a62014-05-23 10:14:19 +010022#include "base/bit_field.h"
Vladimir Marko70e97462016-08-09 11:04:26 +010023#include "base/bit_utils.h"
Nicolas Geoffray39468442014-09-02 15:17:15 +010024#include "base/bit_vector.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070025#include "base/value_object.h"
Nicolas Geoffray76716a62014-05-23 10:14:19 +010026
27namespace art {
28
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010029class HConstant;
Nicolas Geoffray76716a62014-05-23 10:14:19 +010030class HInstruction;
Nicolas Geoffray424f6762014-11-03 14:51:25 +000031class Location;
32
33std::ostream& operator<<(std::ostream& os, const Location& location);
Nicolas Geoffray76716a62014-05-23 10:14:19 +010034
35/**
36 * A Location is an abstraction over the potential location
37 * of an instruction. It could be in register or stack.
38 */
Vladimir Marko76c92ac2015-09-17 15:39:16 +010039class Location : public ValueObject {
Nicolas Geoffray76716a62014-05-23 10:14:19 +010040 public:
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000041 enum OutputOverlap {
Roland Levillain3d312422016-06-23 13:53:42 +010042 // The liveness of the output overlaps the liveness of one or
43 // several input(s); the register allocator cannot reuse an
44 // input's location for the output's location.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000045 kOutputOverlap,
Roland Levillain3d312422016-06-23 13:53:42 +010046 // The liveness of the output does not overlap the liveness of any
47 // input; the register allocator is allowed to reuse an input's
48 // location for the output's location.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000049 kNoOutputOverlap
50 };
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +010051
Nicolas Geoffray76716a62014-05-23 10:14:19 +010052 enum Kind {
53 kInvalid = 0,
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010054 kConstant = 1,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010055 kStackSlot = 2, // 32bit stack slot.
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010056 kDoubleStackSlot = 3, // 64bit stack slot.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010057
58 kRegister = 4, // Core register.
59
60 // We do not use the value 5 because it conflicts with kLocationConstantMask.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +010061 kDoNotUse5 = 5,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010062
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000063 kFpuRegister = 6, // Float register.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010064
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +000065 kRegisterPair = 7, // Long register.
66
67 kFpuRegisterPair = 8, // Double register.
68
69 // We do not use the value 9 because it conflicts with kLocationConstantMask.
70 kDoNotUse9 = 9,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +010071
Nicolas Geoffray76716a62014-05-23 10:14:19 +010072 // Unallocated location represents a location that is not fixed and can be
73 // allocated by a register allocator. Each unallocated location has
74 // a policy that specifies what kind of location is suitable. Payload
75 // contains register allocation policy.
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050076 kUnallocated = 10,
Nicolas Geoffray76716a62014-05-23 10:14:19 +010077 };
78
Vladimir Marko76c92ac2015-09-17 15:39:16 +010079 Location() : ValueObject(), value_(kInvalid) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010080 // Verify that non-constant location kinds do not interfere with kConstant.
Andreas Gampe785d2f22014-11-03 22:57:30 -080081 static_assert((kInvalid & kLocationConstantMask) != kConstant, "TagError");
82 static_assert((kUnallocated & kLocationConstantMask) != kConstant, "TagError");
83 static_assert((kStackSlot & kLocationConstantMask) != kConstant, "TagError");
84 static_assert((kDoubleStackSlot & kLocationConstantMask) != kConstant, "TagError");
85 static_assert((kRegister & kLocationConstantMask) != kConstant, "TagError");
Andreas Gampe785d2f22014-11-03 22:57:30 -080086 static_assert((kFpuRegister & kLocationConstantMask) != kConstant, "TagError");
87 static_assert((kRegisterPair & kLocationConstantMask) != kConstant, "TagError");
88 static_assert((kFpuRegisterPair & kLocationConstantMask) != kConstant, "TagError");
89 static_assert((kConstant & kLocationConstantMask) == kConstant, "TagError");
Nicolas Geoffray96f89a22014-07-11 10:57:49 +010090
Nicolas Geoffray76716a62014-05-23 10:14:19 +010091 DCHECK(!IsValid());
92 }
93
Vladimir Markofa6b93c2015-09-15 10:15:55 +010094 Location(const Location& other) : value_(other.value_) {}
Nicolas Geoffray76716a62014-05-23 10:14:19 +010095
96 Location& operator=(const Location& other) {
97 value_ = other.value_;
98 return *this;
99 }
100
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100101 bool IsConstant() const {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100102 return (value_ & kLocationConstantMask) == kConstant;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100103 }
104
105 static Location ConstantLocation(HConstant* constant) {
106 DCHECK(constant != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700107 return Location(kConstant | reinterpret_cast<uintptr_t>(constant));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100108 }
109
110 HConstant* GetConstant() const {
111 DCHECK(IsConstant());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100112 return reinterpret_cast<HConstant*>(value_ & ~kLocationConstantMask);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100113 }
114
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100115 bool IsValid() const {
116 return value_ != kInvalid;
117 }
118
119 bool IsInvalid() const {
120 return !IsValid();
121 }
122
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100123 // Empty location. Used if there the location should be ignored.
124 static Location NoLocation() {
125 return Location();
126 }
127
128 // Register locations.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100129 static Location RegisterLocation(int reg) {
130 return Location(kRegister, reg);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100131 }
132
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100133 static Location FpuRegisterLocation(int reg) {
134 return Location(kFpuRegister, reg);
135 }
136
137 static Location RegisterPairLocation(int low, int high) {
138 return Location(kRegisterPair, low << 16 | high);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100139 }
140
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000141 static Location FpuRegisterPairLocation(int low, int high) {
142 return Location(kFpuRegisterPair, low << 16 | high);
143 }
144
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100145 bool IsRegister() const {
146 return GetKind() == kRegister;
147 }
148
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100149 bool IsFpuRegister() const {
150 return GetKind() == kFpuRegister;
151 }
152
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100153 bool IsRegisterPair() const {
154 return GetKind() == kRegisterPair;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100155 }
156
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000157 bool IsFpuRegisterPair() const {
158 return GetKind() == kFpuRegisterPair;
159 }
160
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000161 bool IsRegisterKind() const {
162 return IsRegister() || IsFpuRegister() || IsRegisterPair() || IsFpuRegisterPair();
163 }
164
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100165 int reg() const {
166 DCHECK(IsRegister() || IsFpuRegister());
167 return GetPayload();
168 }
169
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000170 int low() const {
171 DCHECK(IsPair());
172 return GetPayload() >> 16;
173 }
174
175 int high() const {
176 DCHECK(IsPair());
177 return GetPayload() & 0xFFFF;
178 }
179
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100180 template <typename T>
Roland Levillain271ab9c2014-11-27 15:23:57 +0000181 T AsRegister() const {
182 DCHECK(IsRegister());
183 return static_cast<T>(reg());
184 }
185
186 template <typename T>
187 T AsFpuRegister() const {
188 DCHECK(IsFpuRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100189 return static_cast<T>(reg());
190 }
191
192 template <typename T>
193 T AsRegisterPairLow() const {
194 DCHECK(IsRegisterPair());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000195 return static_cast<T>(low());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100196 }
197
198 template <typename T>
199 T AsRegisterPairHigh() const {
200 DCHECK(IsRegisterPair());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000201 return static_cast<T>(high());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100202 }
203
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000204 template <typename T>
205 T AsFpuRegisterPairLow() const {
206 DCHECK(IsFpuRegisterPair());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000207 return static_cast<T>(low());
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000208 }
209
210 template <typename T>
211 T AsFpuRegisterPairHigh() const {
212 DCHECK(IsFpuRegisterPair());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000213 return static_cast<T>(high());
214 }
215
216 bool IsPair() const {
217 return IsRegisterPair() || IsFpuRegisterPair();
218 }
219
220 Location ToLow() const {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000221 if (IsRegisterPair()) {
222 return Location::RegisterLocation(low());
223 } else if (IsFpuRegisterPair()) {
224 return Location::FpuRegisterLocation(low());
225 } else {
226 DCHECK(IsDoubleStackSlot());
227 return Location::StackSlot(GetStackIndex());
228 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000229 }
230
231 Location ToHigh() const {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000232 if (IsRegisterPair()) {
233 return Location::RegisterLocation(high());
234 } else if (IsFpuRegisterPair()) {
235 return Location::FpuRegisterLocation(high());
236 } else {
237 DCHECK(IsDoubleStackSlot());
238 return Location::StackSlot(GetHighStackIndex(4));
239 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000240 }
241
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100242 static uintptr_t EncodeStackIndex(intptr_t stack_index) {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100243 DCHECK(-kStackIndexBias <= stack_index);
244 DCHECK(stack_index < kStackIndexBias);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100245 return static_cast<uintptr_t>(kStackIndexBias + stack_index);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100246 }
247
248 static Location StackSlot(intptr_t stack_index) {
Ian Rogers13735952014-10-08 12:43:28 -0700249 uintptr_t payload = EncodeStackIndex(stack_index);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100250 Location loc(kStackSlot, payload);
251 // Ensure that sign is preserved.
252 DCHECK_EQ(loc.GetStackIndex(), stack_index);
253 return loc;
254 }
255
256 bool IsStackSlot() const {
257 return GetKind() == kStackSlot;
258 }
259
260 static Location DoubleStackSlot(intptr_t stack_index) {
Ian Rogers13735952014-10-08 12:43:28 -0700261 uintptr_t payload = EncodeStackIndex(stack_index);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100262 Location loc(kDoubleStackSlot, payload);
263 // Ensure that sign is preserved.
264 DCHECK_EQ(loc.GetStackIndex(), stack_index);
265 return loc;
266 }
267
268 bool IsDoubleStackSlot() const {
269 return GetKind() == kDoubleStackSlot;
270 }
271
272 intptr_t GetStackIndex() const {
273 DCHECK(IsStackSlot() || IsDoubleStackSlot());
274 // Decode stack index manually to preserve sign.
275 return GetPayload() - kStackIndexBias;
276 }
277
278 intptr_t GetHighStackIndex(uintptr_t word_size) const {
279 DCHECK(IsDoubleStackSlot());
280 // Decode stack index manually to preserve sign.
281 return GetPayload() - kStackIndexBias + word_size;
282 }
283
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100284 Kind GetKind() const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100285 return IsConstant() ? kConstant : KindField::Decode(value_);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100286 }
287
288 bool Equals(Location other) const {
289 return value_ == other.value_;
290 }
291
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000292 bool Contains(Location other) const {
293 if (Equals(other)) {
294 return true;
Zheng Xuad4450e2015-04-17 18:48:56 +0800295 } else if (IsPair() || IsDoubleStackSlot()) {
296 return ToLow().Equals(other) || ToHigh().Equals(other);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000297 }
298 return false;
299 }
300
Zheng Xuad4450e2015-04-17 18:48:56 +0800301 bool OverlapsWith(Location other) const {
302 // Only check the overlapping case that can happen with our register allocation algorithm.
303 bool overlap = Contains(other) || other.Contains(*this);
304 if (kIsDebugBuild && !overlap) {
305 // Note: These are also overlapping cases. But we are not able to handle them in
306 // ParallelMoveResolverWithSwap. Make sure that we do not meet such case with our compiler.
307 if ((IsPair() && other.IsPair()) || (IsDoubleStackSlot() && other.IsDoubleStackSlot())) {
308 DCHECK(!Contains(other.ToLow()));
309 DCHECK(!Contains(other.ToHigh()));
310 }
311 }
312 return overlap;
313 }
314
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100315 const char* DebugString() const {
316 switch (GetKind()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100317 case kInvalid: return "I";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100318 case kRegister: return "R";
319 case kStackSlot: return "S";
320 case kDoubleStackSlot: return "DS";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100321 case kUnallocated: return "U";
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100322 case kConstant: return "C";
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100323 case kFpuRegister: return "F";
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100324 case kRegisterPair: return "RP";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000325 case kFpuRegisterPair: return "FP";
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100326 case kDoNotUse5: // fall-through
327 case kDoNotUse9:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100328 LOG(FATAL) << "Should not use this location kind";
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100329 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100330 UNREACHABLE();
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100331 return "?";
332 }
333
334 // Unallocated locations.
335 enum Policy {
336 kAny,
337 kRequiresRegister,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100338 kRequiresFpuRegister,
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100339 kSameAsFirstInput,
340 };
341
342 bool IsUnallocated() const {
343 return GetKind() == kUnallocated;
344 }
345
346 static Location UnallocatedLocation(Policy policy) {
347 return Location(kUnallocated, PolicyField::Encode(policy));
348 }
349
350 // Any free register is suitable to replace this unallocated location.
351 static Location Any() {
352 return UnallocatedLocation(kAny);
353 }
354
355 static Location RequiresRegister() {
356 return UnallocatedLocation(kRequiresRegister);
357 }
358
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100359 static Location RequiresFpuRegister() {
360 return UnallocatedLocation(kRequiresFpuRegister);
361 }
362
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100363 static Location RegisterOrConstant(HInstruction* instruction);
Mark Mendellea5af682015-10-22 17:35:49 -0400364 static Location RegisterOrInt32Constant(HInstruction* instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100365 static Location ByteRegisterOrConstant(int reg, HInstruction* instruction);
Mark Mendellea5af682015-10-22 17:35:49 -0400366 static Location FpuRegisterOrConstant(HInstruction* instruction);
367 static Location FpuRegisterOrInt32Constant(HInstruction* instruction);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100368
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100369 // The location of the first input to the instruction will be
370 // used to replace this unallocated location.
371 static Location SameAsFirstInput() {
372 return UnallocatedLocation(kSameAsFirstInput);
373 }
374
375 Policy GetPolicy() const {
376 DCHECK(IsUnallocated());
377 return PolicyField::Decode(GetPayload());
378 }
379
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700380 bool RequiresRegisterKind() const {
381 return GetPolicy() == kRequiresRegister || GetPolicy() == kRequiresFpuRegister;
382 }
383
Ian Rogers13735952014-10-08 12:43:28 -0700384 uintptr_t GetEncoding() const {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100385 return GetPayload();
386 }
387
388 private:
389 // Number of bits required to encode Kind value.
390 static constexpr uint32_t kBitsForKind = 4;
Ian Rogers13735952014-10-08 12:43:28 -0700391 static constexpr uint32_t kBitsForPayload = kBitsPerIntPtrT - kBitsForKind;
392 static constexpr uintptr_t kLocationConstantMask = 0x3;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100393
Ian Rogers13735952014-10-08 12:43:28 -0700394 explicit Location(uintptr_t value) : value_(value) {}
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100395
Ian Rogers13735952014-10-08 12:43:28 -0700396 Location(Kind kind, uintptr_t payload)
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100397 : value_(KindField::Encode(kind) | PayloadField::Encode(payload)) {}
398
Ian Rogers13735952014-10-08 12:43:28 -0700399 uintptr_t GetPayload() const {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100400 return PayloadField::Decode(value_);
401 }
402
403 typedef BitField<Kind, 0, kBitsForKind> KindField;
Ian Rogers13735952014-10-08 12:43:28 -0700404 typedef BitField<uintptr_t, kBitsForKind, kBitsForPayload> PayloadField;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100405
406 // Layout for kUnallocated locations payload.
407 typedef BitField<Policy, 0, 3> PolicyField;
408
409 // Layout for stack slots.
410 static const intptr_t kStackIndexBias =
411 static_cast<intptr_t>(1) << (kBitsForPayload - 1);
412
413 // Location either contains kind and payload fields or a tagged handle for
414 // a constant locations. Values of enumeration Kind are selected in such a
415 // way that none of them can be interpreted as a kConstant tag.
Ian Rogers13735952014-10-08 12:43:28 -0700416 uintptr_t value_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100417};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700418std::ostream& operator<<(std::ostream& os, const Location::Kind& rhs);
419std::ostream& operator<<(std::ostream& os, const Location::Policy& rhs);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100420
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100421class RegisterSet : public ValueObject {
422 public:
Vladimir Marko804b03f2016-09-14 16:26:36 +0100423 static RegisterSet Empty() { return RegisterSet(); }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100424
425 void Add(Location loc) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100426 if (loc.IsRegister()) {
427 core_registers_ |= (1 << loc.reg());
428 } else {
429 DCHECK(loc.IsFpuRegister());
430 floating_point_registers_ |= (1 << loc.reg());
431 }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100432 }
433
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100434 void Remove(Location loc) {
435 if (loc.IsRegister()) {
436 core_registers_ &= ~(1 << loc.reg());
437 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000438 DCHECK(loc.IsFpuRegister()) << loc;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100439 floating_point_registers_ &= ~(1 << loc.reg());
440 }
441 }
442
Nicolas Geoffray45b83af2015-07-06 15:12:53 +0000443 bool ContainsCoreRegister(uint32_t id) const {
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100444 return Contains(core_registers_, id);
445 }
446
Nicolas Geoffray45b83af2015-07-06 15:12:53 +0000447 bool ContainsFloatingPointRegister(uint32_t id) const {
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100448 return Contains(floating_point_registers_, id);
449 }
450
451 static bool Contains(uint32_t register_set, uint32_t reg) {
452 return (register_set & (1 << reg)) != 0;
453 }
454
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000455 size_t GetNumberOfRegisters() const {
Vladimir Marko70e97462016-08-09 11:04:26 +0100456 return POPCOUNT(core_registers_) + POPCOUNT(floating_point_registers_);
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000457 }
458
Nicolas Geoffray98893962015-01-21 12:32:32 +0000459 uint32_t GetCoreRegisters() const {
460 return core_registers_;
461 }
462
463 uint32_t GetFloatingPointRegisters() const {
464 return floating_point_registers_;
465 }
466
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100467 private:
Vladimir Marko804b03f2016-09-14 16:26:36 +0100468 RegisterSet() : core_registers_(0), floating_point_registers_(0) {}
469
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100470 uint32_t core_registers_;
471 uint32_t floating_point_registers_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100472};
473
Andreas Gampe878d58c2015-01-15 23:24:00 -0800474static constexpr bool kIntrinsified = true;
475
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100476/**
477 * The code generator computes LocationSummary for each instruction so that
478 * the instruction itself knows what code to generate: where to find the inputs
479 * and where to place the result.
480 *
481 * The intent is to have the code for generating the instruction independent of
482 * register allocation. A register allocator just has to provide a LocationSummary.
483 */
Vladimir Marko5233f932015-09-29 19:01:15 +0100484class LocationSummary : public ArenaObject<kArenaAllocLocationSummary> {
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100485 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100486 enum CallKind {
487 kNoCall,
Serban Constantinescu806f0122016-03-09 11:10:16 +0000488 kCallOnMainAndSlowPath,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100489 kCallOnSlowPath,
Serban Constantinescu54ff4822016-07-07 18:03:19 +0100490 kCallOnMainOnly
Nicolas Geoffray39468442014-09-02 15:17:15 +0100491 };
492
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700493 explicit LocationSummary(HInstruction* instruction,
494 CallKind call_kind = kNoCall,
495 bool intrinsified = false);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100496
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100497 void SetInAt(uint32_t at, Location location) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100498 inputs_[at] = location;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100499 }
500
501 Location InAt(uint32_t at) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100502 return inputs_[at];
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100503 }
504
505 size_t GetInputCount() const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100506 return inputs_.size();
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100507 }
508
Roland Levillain3d312422016-06-23 13:53:42 +0100509 // Set the output location. Argument `overlaps` tells whether the
510 // output overlaps any of the inputs (if so, it cannot share the
511 // same register as one of the inputs); it is set to
512 // `Location::kOutputOverlap` by default for safety.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000513 void SetOut(Location location, Location::OutputOverlap overlaps = Location::kOutputOverlap) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000514 DCHECK(output_.IsInvalid());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100515 output_overlaps_ = overlaps;
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000516 output_ = location;
517 }
518
519 void UpdateOut(Location location) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000520 // There are two reasons for updating an output:
521 // 1) Parameters, where we only know the exact stack slot after
522 // doing full register allocation.
523 // 2) Unallocated location.
524 DCHECK(output_.IsStackSlot() || output_.IsDoubleStackSlot() || output_.IsUnallocated());
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000525 output_ = location;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100526 }
527
528 void AddTemp(Location location) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100529 temps_.push_back(location);
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100530 }
531
532 Location GetTemp(uint32_t at) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100533 return temps_[at];
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100534 }
535
536 void SetTempAt(uint32_t at, Location location) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100537 DCHECK(temps_[at].IsUnallocated() || temps_[at].IsInvalid());
538 temps_[at] = location;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100539 }
540
541 size_t GetTempCount() const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100542 return temps_.size();
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100543 }
544
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100545 bool HasTemps() const { return !temps_.empty(); }
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100546
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100547 Location Out() const { return output_; }
548
Serban Constantinescu806f0122016-03-09 11:10:16 +0000549 bool CanCall() const {
550 return call_kind_ != kNoCall;
551 }
552
553 bool WillCall() const {
554 return call_kind_ == kCallOnMainOnly || call_kind_ == kCallOnMainAndSlowPath;
555 }
556
557 bool CallsOnSlowPath() const {
558 return call_kind_ == kCallOnSlowPath || call_kind_ == kCallOnMainAndSlowPath;
559 }
560
561 bool OnlyCallsOnSlowPath() const {
562 return call_kind_ == kCallOnSlowPath;
563 }
564
565 bool CallsOnMainAndSlowPath() const {
566 return call_kind_ == kCallOnMainAndSlowPath;
567 }
568
569 bool NeedsSafepoint() const {
570 return CanCall();
571 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100572
Vladimir Marko70e97462016-08-09 11:04:26 +0100573 void SetCustomSlowPathCallerSaves(const RegisterSet& caller_saves) {
574 DCHECK(OnlyCallsOnSlowPath());
575 has_custom_slow_path_calling_convention_ = true;
576 custom_slow_path_caller_saves_ = caller_saves;
577 }
578
579 bool HasCustomSlowPathCallingConvention() const {
580 return has_custom_slow_path_calling_convention_;
581 }
582
583 const RegisterSet& GetCustomSlowPathCallerSaves() const {
584 DCHECK(HasCustomSlowPathCallingConvention());
585 return custom_slow_path_caller_saves_;
586 }
587
Nicolas Geoffray39468442014-09-02 15:17:15 +0100588 void SetStackBit(uint32_t index) {
589 stack_mask_->SetBit(index);
590 }
591
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100592 void ClearStackBit(uint32_t index) {
593 stack_mask_->ClearBit(index);
594 }
595
Nicolas Geoffray39468442014-09-02 15:17:15 +0100596 void SetRegisterBit(uint32_t reg_id) {
597 register_mask_ |= (1 << reg_id);
598 }
599
Nicolas Geoffray98893962015-01-21 12:32:32 +0000600 uint32_t GetRegisterMask() const {
601 return register_mask_;
602 }
603
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100604 bool RegisterContainsObject(uint32_t reg_id) {
605 return RegisterSet::Contains(register_mask_, reg_id);
606 }
607
608 void AddLiveRegister(Location location) {
609 live_registers_.Add(location);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100610 }
611
612 BitVector* GetStackMask() const {
613 return stack_mask_;
614 }
615
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100616 RegisterSet* GetLiveRegisters() {
617 return &live_registers_;
618 }
619
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000620 size_t GetNumberOfLiveRegisters() const {
621 return live_registers_.GetNumberOfRegisters();
622 }
623
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000624 bool OutputUsesSameAs(uint32_t input_index) const {
625 return (input_index == 0)
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100626 && output_.IsUnallocated()
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000627 && (output_.GetPolicy() == Location::kSameAsFirstInput);
628 }
629
630 bool IsFixedInput(uint32_t input_index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100631 Location input = inputs_[input_index];
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000632 return input.IsRegister()
Nicolas Geoffray98893962015-01-21 12:32:32 +0000633 || input.IsFpuRegister()
634 || input.IsPair()
635 || input.IsStackSlot()
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000636 || input.IsDoubleStackSlot();
Nicolas Geoffray76905622014-09-25 14:39:26 +0100637 }
638
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000639 bool OutputCanOverlapWithInputs() const {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000640 return output_overlaps_ == Location::kOutputOverlap;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100641 }
642
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800643 bool Intrinsified() const {
644 return intrinsified_;
645 }
646
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100647 private:
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100648 ArenaVector<Location> inputs_;
649 ArenaVector<Location> temps_;
Vladimir Marko70e97462016-08-09 11:04:26 +0100650 const CallKind call_kind_;
651 // Whether these are locations for an intrinsified call.
652 const bool intrinsified_;
653 // Whether the slow path has default or custom calling convention.
654 bool has_custom_slow_path_calling_convention_;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100655 // Whether the output overlaps with any of the inputs. If it overlaps, then it cannot
656 // share the same register as the inputs.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000657 Location::OutputOverlap output_overlaps_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100658 Location output_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100659
660 // Mask of objects that live in the stack.
661 BitVector* stack_mask_;
662
663 // Mask of objects that live in register.
664 uint32_t register_mask_;
665
666 // Registers that are in use at this position.
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100667 RegisterSet live_registers_;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100668
Vladimir Marko70e97462016-08-09 11:04:26 +0100669 // Custom slow path caller saves. Valid only if indicated by slow_path_calling_convention_.
670 RegisterSet custom_slow_path_caller_saves_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800671
Matthew Gharrity2ac06bc2016-08-05 09:34:52 -0700672 friend class RegisterAllocatorTest;
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100673 DISALLOW_COPY_AND_ASSIGN(LocationSummary);
674};
675
Nicolas Geoffray76716a62014-05-23 10:14:19 +0100676} // namespace art
677
678#endif // ART_COMPILER_OPTIMIZING_LOCATIONS_H_