Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 20 | #include "base/arena_object.h" |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 21 | #include "base/bit_field.h" |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 22 | #include "base/bit_vector.h" |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 23 | #include "base/value_object.h" |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 24 | #include "utils/growable_array.h" |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 28 | class HConstant; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 29 | class HInstruction; |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 30 | class Location; |
| 31 | |
| 32 | std::ostream& operator<<(std::ostream& os, const Location& location); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * A Location is an abstraction over the potential location |
| 36 | * of an instruction. It could be in register or stack. |
| 37 | */ |
Vladimir Marko | 76c92ac | 2015-09-17 15:39:16 +0100 | [diff] [blame^] | 38 | class Location : public ValueObject { |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 39 | public: |
Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 40 | enum OutputOverlap { |
| 41 | kOutputOverlap, |
| 42 | kNoOutputOverlap |
| 43 | }; |
Nicolas Geoffray | 9ae0daa | 2014-09-30 22:40:23 +0100 | [diff] [blame] | 44 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 45 | enum Kind { |
| 46 | kInvalid = 0, |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 47 | kConstant = 1, |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 48 | kStackSlot = 2, // 32bit stack slot. |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 49 | kDoubleStackSlot = 3, // 64bit stack slot. |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 50 | |
| 51 | kRegister = 4, // Core register. |
| 52 | |
| 53 | // We do not use the value 5 because it conflicts with kLocationConstantMask. |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 54 | kDoNotUse5 = 5, |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 55 | |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 56 | kFpuRegister = 6, // Float register. |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 57 | |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 58 | kRegisterPair = 7, // Long register. |
| 59 | |
| 60 | kFpuRegisterPair = 8, // Double register. |
| 61 | |
| 62 | // We do not use the value 9 because it conflicts with kLocationConstantMask. |
| 63 | kDoNotUse9 = 9, |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 64 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 65 | // Unallocated location represents a location that is not fixed and can be |
| 66 | // allocated by a register allocator. Each unallocated location has |
| 67 | // a policy that specifies what kind of location is suitable. Payload |
| 68 | // contains register allocation policy. |
Mark Mendell | 3e6a3bf | 2015-01-19 14:09:22 -0500 | [diff] [blame] | 69 | kUnallocated = 10, |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 70 | }; |
| 71 | |
Vladimir Marko | 76c92ac | 2015-09-17 15:39:16 +0100 | [diff] [blame^] | 72 | Location() : ValueObject(), value_(kInvalid) { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 73 | // Verify that non-constant location kinds do not interfere with kConstant. |
Andreas Gampe | 785d2f2 | 2014-11-03 22:57:30 -0800 | [diff] [blame] | 74 | static_assert((kInvalid & kLocationConstantMask) != kConstant, "TagError"); |
| 75 | static_assert((kUnallocated & kLocationConstantMask) != kConstant, "TagError"); |
| 76 | static_assert((kStackSlot & kLocationConstantMask) != kConstant, "TagError"); |
| 77 | static_assert((kDoubleStackSlot & kLocationConstantMask) != kConstant, "TagError"); |
| 78 | static_assert((kRegister & kLocationConstantMask) != kConstant, "TagError"); |
Andreas Gampe | 785d2f2 | 2014-11-03 22:57:30 -0800 | [diff] [blame] | 79 | static_assert((kFpuRegister & kLocationConstantMask) != kConstant, "TagError"); |
| 80 | static_assert((kRegisterPair & kLocationConstantMask) != kConstant, "TagError"); |
| 81 | static_assert((kFpuRegisterPair & kLocationConstantMask) != kConstant, "TagError"); |
| 82 | static_assert((kConstant & kLocationConstantMask) == kConstant, "TagError"); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 83 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 84 | DCHECK(!IsValid()); |
| 85 | } |
| 86 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 87 | Location(const Location& other) : value_(other.value_) {} |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 88 | |
| 89 | Location& operator=(const Location& other) { |
| 90 | value_ = other.value_; |
| 91 | return *this; |
| 92 | } |
| 93 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 94 | bool IsConstant() const { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 95 | return (value_ & kLocationConstantMask) == kConstant; |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static Location ConstantLocation(HConstant* constant) { |
| 99 | DCHECK(constant != nullptr); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 100 | return Location(kConstant | reinterpret_cast<uintptr_t>(constant)); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | HConstant* GetConstant() const { |
| 104 | DCHECK(IsConstant()); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 105 | return reinterpret_cast<HConstant*>(value_ & ~kLocationConstantMask); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 108 | bool IsValid() const { |
| 109 | return value_ != kInvalid; |
| 110 | } |
| 111 | |
| 112 | bool IsInvalid() const { |
| 113 | return !IsValid(); |
| 114 | } |
| 115 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 116 | // Empty location. Used if there the location should be ignored. |
| 117 | static Location NoLocation() { |
| 118 | return Location(); |
| 119 | } |
| 120 | |
| 121 | // Register locations. |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 122 | static Location RegisterLocation(int reg) { |
| 123 | return Location(kRegister, reg); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 126 | static Location FpuRegisterLocation(int reg) { |
| 127 | return Location(kFpuRegister, reg); |
| 128 | } |
| 129 | |
| 130 | static Location RegisterPairLocation(int low, int high) { |
| 131 | return Location(kRegisterPair, low << 16 | high); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 134 | static Location FpuRegisterPairLocation(int low, int high) { |
| 135 | return Location(kFpuRegisterPair, low << 16 | high); |
| 136 | } |
| 137 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 138 | bool IsRegister() const { |
| 139 | return GetKind() == kRegister; |
| 140 | } |
| 141 | |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 142 | bool IsFpuRegister() const { |
| 143 | return GetKind() == kFpuRegister; |
| 144 | } |
| 145 | |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 146 | bool IsRegisterPair() const { |
| 147 | return GetKind() == kRegisterPair; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 150 | bool IsFpuRegisterPair() const { |
| 151 | return GetKind() == kFpuRegisterPair; |
| 152 | } |
| 153 | |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 154 | bool IsRegisterKind() const { |
| 155 | return IsRegister() || IsFpuRegister() || IsRegisterPair() || IsFpuRegisterPair(); |
| 156 | } |
| 157 | |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 158 | int reg() const { |
| 159 | DCHECK(IsRegister() || IsFpuRegister()); |
| 160 | return GetPayload(); |
| 161 | } |
| 162 | |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 163 | int low() const { |
| 164 | DCHECK(IsPair()); |
| 165 | return GetPayload() >> 16; |
| 166 | } |
| 167 | |
| 168 | int high() const { |
| 169 | DCHECK(IsPair()); |
| 170 | return GetPayload() & 0xFFFF; |
| 171 | } |
| 172 | |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 173 | template <typename T> |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 174 | T AsRegister() const { |
| 175 | DCHECK(IsRegister()); |
| 176 | return static_cast<T>(reg()); |
| 177 | } |
| 178 | |
| 179 | template <typename T> |
| 180 | T AsFpuRegister() const { |
| 181 | DCHECK(IsFpuRegister()); |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 182 | return static_cast<T>(reg()); |
| 183 | } |
| 184 | |
| 185 | template <typename T> |
| 186 | T AsRegisterPairLow() const { |
| 187 | DCHECK(IsRegisterPair()); |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 188 | return static_cast<T>(low()); |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | template <typename T> |
| 192 | T AsRegisterPairHigh() const { |
| 193 | DCHECK(IsRegisterPair()); |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 194 | return static_cast<T>(high()); |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 197 | template <typename T> |
| 198 | T AsFpuRegisterPairLow() const { |
| 199 | DCHECK(IsFpuRegisterPair()); |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 200 | return static_cast<T>(low()); |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | template <typename T> |
| 204 | T AsFpuRegisterPairHigh() const { |
| 205 | DCHECK(IsFpuRegisterPair()); |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 206 | return static_cast<T>(high()); |
| 207 | } |
| 208 | |
| 209 | bool IsPair() const { |
| 210 | return IsRegisterPair() || IsFpuRegisterPair(); |
| 211 | } |
| 212 | |
| 213 | Location ToLow() const { |
Nicolas Geoffray | 234d69d | 2015-03-09 10:28:50 +0000 | [diff] [blame] | 214 | if (IsRegisterPair()) { |
| 215 | return Location::RegisterLocation(low()); |
| 216 | } else if (IsFpuRegisterPair()) { |
| 217 | return Location::FpuRegisterLocation(low()); |
| 218 | } else { |
| 219 | DCHECK(IsDoubleStackSlot()); |
| 220 | return Location::StackSlot(GetStackIndex()); |
| 221 | } |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | Location ToHigh() const { |
Nicolas Geoffray | 234d69d | 2015-03-09 10:28:50 +0000 | [diff] [blame] | 225 | if (IsRegisterPair()) { |
| 226 | return Location::RegisterLocation(high()); |
| 227 | } else if (IsFpuRegisterPair()) { |
| 228 | return Location::FpuRegisterLocation(high()); |
| 229 | } else { |
| 230 | DCHECK(IsDoubleStackSlot()); |
| 231 | return Location::StackSlot(GetHighStackIndex(4)); |
| 232 | } |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 235 | static uintptr_t EncodeStackIndex(intptr_t stack_index) { |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 236 | DCHECK(-kStackIndexBias <= stack_index); |
| 237 | DCHECK(stack_index < kStackIndexBias); |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 238 | return static_cast<uintptr_t>(kStackIndexBias + stack_index); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static Location StackSlot(intptr_t stack_index) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 242 | uintptr_t payload = EncodeStackIndex(stack_index); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 243 | Location loc(kStackSlot, payload); |
| 244 | // Ensure that sign is preserved. |
| 245 | DCHECK_EQ(loc.GetStackIndex(), stack_index); |
| 246 | return loc; |
| 247 | } |
| 248 | |
| 249 | bool IsStackSlot() const { |
| 250 | return GetKind() == kStackSlot; |
| 251 | } |
| 252 | |
| 253 | static Location DoubleStackSlot(intptr_t stack_index) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 254 | uintptr_t payload = EncodeStackIndex(stack_index); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 255 | Location loc(kDoubleStackSlot, payload); |
| 256 | // Ensure that sign is preserved. |
| 257 | DCHECK_EQ(loc.GetStackIndex(), stack_index); |
| 258 | return loc; |
| 259 | } |
| 260 | |
| 261 | bool IsDoubleStackSlot() const { |
| 262 | return GetKind() == kDoubleStackSlot; |
| 263 | } |
| 264 | |
| 265 | intptr_t GetStackIndex() const { |
| 266 | DCHECK(IsStackSlot() || IsDoubleStackSlot()); |
| 267 | // Decode stack index manually to preserve sign. |
| 268 | return GetPayload() - kStackIndexBias; |
| 269 | } |
| 270 | |
| 271 | intptr_t GetHighStackIndex(uintptr_t word_size) const { |
| 272 | DCHECK(IsDoubleStackSlot()); |
| 273 | // Decode stack index manually to preserve sign. |
| 274 | return GetPayload() - kStackIndexBias + word_size; |
| 275 | } |
| 276 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 277 | Kind GetKind() const { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 278 | return IsConstant() ? kConstant : KindField::Decode(value_); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | bool Equals(Location other) const { |
| 282 | return value_ == other.value_; |
| 283 | } |
| 284 | |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 285 | bool Contains(Location other) const { |
| 286 | if (Equals(other)) { |
| 287 | return true; |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 288 | } else if (IsPair() || IsDoubleStackSlot()) { |
| 289 | return ToLow().Equals(other) || ToHigh().Equals(other); |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 290 | } |
| 291 | return false; |
| 292 | } |
| 293 | |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 294 | bool OverlapsWith(Location other) const { |
| 295 | // Only check the overlapping case that can happen with our register allocation algorithm. |
| 296 | bool overlap = Contains(other) || other.Contains(*this); |
| 297 | if (kIsDebugBuild && !overlap) { |
| 298 | // Note: These are also overlapping cases. But we are not able to handle them in |
| 299 | // ParallelMoveResolverWithSwap. Make sure that we do not meet such case with our compiler. |
| 300 | if ((IsPair() && other.IsPair()) || (IsDoubleStackSlot() && other.IsDoubleStackSlot())) { |
| 301 | DCHECK(!Contains(other.ToLow())); |
| 302 | DCHECK(!Contains(other.ToHigh())); |
| 303 | } |
| 304 | } |
| 305 | return overlap; |
| 306 | } |
| 307 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 308 | const char* DebugString() const { |
| 309 | switch (GetKind()) { |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 310 | case kInvalid: return "I"; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 311 | case kRegister: return "R"; |
| 312 | case kStackSlot: return "S"; |
| 313 | case kDoubleStackSlot: return "DS"; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 314 | case kUnallocated: return "U"; |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 315 | case kConstant: return "C"; |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 316 | case kFpuRegister: return "F"; |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 317 | case kRegisterPair: return "RP"; |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 318 | case kFpuRegisterPair: return "FP"; |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 319 | case kDoNotUse5: // fall-through |
| 320 | case kDoNotUse9: |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 321 | LOG(FATAL) << "Should not use this location kind"; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 322 | } |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 323 | UNREACHABLE(); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 324 | return "?"; |
| 325 | } |
| 326 | |
| 327 | // Unallocated locations. |
| 328 | enum Policy { |
| 329 | kAny, |
| 330 | kRequiresRegister, |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 331 | kRequiresFpuRegister, |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 332 | kSameAsFirstInput, |
| 333 | }; |
| 334 | |
| 335 | bool IsUnallocated() const { |
| 336 | return GetKind() == kUnallocated; |
| 337 | } |
| 338 | |
| 339 | static Location UnallocatedLocation(Policy policy) { |
| 340 | return Location(kUnallocated, PolicyField::Encode(policy)); |
| 341 | } |
| 342 | |
| 343 | // Any free register is suitable to replace this unallocated location. |
| 344 | static Location Any() { |
| 345 | return UnallocatedLocation(kAny); |
| 346 | } |
| 347 | |
| 348 | static Location RequiresRegister() { |
| 349 | return UnallocatedLocation(kRequiresRegister); |
| 350 | } |
| 351 | |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 352 | static Location RequiresFpuRegister() { |
| 353 | return UnallocatedLocation(kRequiresFpuRegister); |
| 354 | } |
| 355 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 356 | static Location RegisterOrConstant(HInstruction* instruction); |
Mark Mendell | 3f6c7f6 | 2015-03-13 13:47:53 -0400 | [diff] [blame] | 357 | static Location RegisterOrInt32LongConstant(HInstruction* instruction); |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 358 | static Location ByteRegisterOrConstant(int reg, HInstruction* instruction); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 359 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 360 | // The location of the first input to the instruction will be |
| 361 | // used to replace this unallocated location. |
| 362 | static Location SameAsFirstInput() { |
| 363 | return UnallocatedLocation(kSameAsFirstInput); |
| 364 | } |
| 365 | |
| 366 | Policy GetPolicy() const { |
| 367 | DCHECK(IsUnallocated()); |
| 368 | return PolicyField::Decode(GetPayload()); |
| 369 | } |
| 370 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 371 | uintptr_t GetEncoding() const { |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 372 | return GetPayload(); |
| 373 | } |
| 374 | |
| 375 | private: |
| 376 | // Number of bits required to encode Kind value. |
| 377 | static constexpr uint32_t kBitsForKind = 4; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 378 | static constexpr uint32_t kBitsForPayload = kBitsPerIntPtrT - kBitsForKind; |
| 379 | static constexpr uintptr_t kLocationConstantMask = 0x3; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 380 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 381 | explicit Location(uintptr_t value) : value_(value) {} |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 382 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 383 | Location(Kind kind, uintptr_t payload) |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 384 | : value_(KindField::Encode(kind) | PayloadField::Encode(payload)) {} |
| 385 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 386 | uintptr_t GetPayload() const { |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 387 | return PayloadField::Decode(value_); |
| 388 | } |
| 389 | |
| 390 | typedef BitField<Kind, 0, kBitsForKind> KindField; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 391 | typedef BitField<uintptr_t, kBitsForKind, kBitsForPayload> PayloadField; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 392 | |
| 393 | // Layout for kUnallocated locations payload. |
| 394 | typedef BitField<Policy, 0, 3> PolicyField; |
| 395 | |
| 396 | // Layout for stack slots. |
| 397 | static const intptr_t kStackIndexBias = |
| 398 | static_cast<intptr_t>(1) << (kBitsForPayload - 1); |
| 399 | |
| 400 | // Location either contains kind and payload fields or a tagged handle for |
| 401 | // a constant locations. Values of enumeration Kind are selected in such a |
| 402 | // way that none of them can be interpreted as a kConstant tag. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 403 | uintptr_t value_; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 404 | }; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 405 | std::ostream& operator<<(std::ostream& os, const Location::Kind& rhs); |
| 406 | std::ostream& operator<<(std::ostream& os, const Location::Policy& rhs); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 407 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 408 | class RegisterSet : public ValueObject { |
| 409 | public: |
| 410 | RegisterSet() : core_registers_(0), floating_point_registers_(0) {} |
| 411 | |
| 412 | void Add(Location loc) { |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 413 | if (loc.IsRegister()) { |
| 414 | core_registers_ |= (1 << loc.reg()); |
| 415 | } else { |
| 416 | DCHECK(loc.IsFpuRegister()); |
| 417 | floating_point_registers_ |= (1 << loc.reg()); |
| 418 | } |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 419 | } |
| 420 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 421 | void Remove(Location loc) { |
| 422 | if (loc.IsRegister()) { |
| 423 | core_registers_ &= ~(1 << loc.reg()); |
| 424 | } else { |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 425 | DCHECK(loc.IsFpuRegister()) << loc; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 426 | floating_point_registers_ &= ~(1 << loc.reg()); |
| 427 | } |
| 428 | } |
| 429 | |
Nicolas Geoffray | 45b83af | 2015-07-06 15:12:53 +0000 | [diff] [blame] | 430 | bool ContainsCoreRegister(uint32_t id) const { |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 431 | return Contains(core_registers_, id); |
| 432 | } |
| 433 | |
Nicolas Geoffray | 45b83af | 2015-07-06 15:12:53 +0000 | [diff] [blame] | 434 | bool ContainsFloatingPointRegister(uint32_t id) const { |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 435 | return Contains(floating_point_registers_, id); |
| 436 | } |
| 437 | |
| 438 | static bool Contains(uint32_t register_set, uint32_t reg) { |
| 439 | return (register_set & (1 << reg)) != 0; |
| 440 | } |
| 441 | |
Nicolas Geoffray | 87d0376 | 2014-11-19 15:17:56 +0000 | [diff] [blame] | 442 | size_t GetNumberOfRegisters() const { |
| 443 | return __builtin_popcount(core_registers_) + __builtin_popcount(floating_point_registers_); |
| 444 | } |
| 445 | |
Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 446 | uint32_t GetCoreRegisters() const { |
| 447 | return core_registers_; |
| 448 | } |
| 449 | |
| 450 | uint32_t GetFloatingPointRegisters() const { |
| 451 | return floating_point_registers_; |
| 452 | } |
| 453 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 454 | private: |
| 455 | uint32_t core_registers_; |
| 456 | uint32_t floating_point_registers_; |
| 457 | |
| 458 | DISALLOW_COPY_AND_ASSIGN(RegisterSet); |
| 459 | }; |
| 460 | |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 461 | static constexpr bool kIntrinsified = true; |
| 462 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 463 | /** |
| 464 | * The code generator computes LocationSummary for each instruction so that |
| 465 | * the instruction itself knows what code to generate: where to find the inputs |
| 466 | * and where to place the result. |
| 467 | * |
| 468 | * The intent is to have the code for generating the instruction independent of |
| 469 | * register allocation. A register allocator just has to provide a LocationSummary. |
| 470 | */ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 471 | class LocationSummary : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 472 | public: |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 473 | enum CallKind { |
| 474 | kNoCall, |
| 475 | kCallOnSlowPath, |
| 476 | kCall |
| 477 | }; |
| 478 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 479 | LocationSummary(HInstruction* instruction, |
| 480 | CallKind call_kind = kNoCall, |
| 481 | bool intrinsified = false); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 482 | |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 483 | void SetInAt(uint32_t at, Location location) { |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 484 | inputs_.Put(at, location); |
| 485 | } |
| 486 | |
| 487 | Location InAt(uint32_t at) const { |
| 488 | return inputs_.Get(at); |
| 489 | } |
| 490 | |
| 491 | size_t GetInputCount() const { |
| 492 | return inputs_.Size(); |
| 493 | } |
| 494 | |
Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 495 | void SetOut(Location location, Location::OutputOverlap overlaps = Location::kOutputOverlap) { |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 496 | DCHECK(output_.IsInvalid()); |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 497 | output_overlaps_ = overlaps; |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 498 | output_ = location; |
| 499 | } |
| 500 | |
| 501 | void UpdateOut(Location location) { |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 502 | // There are two reasons for updating an output: |
| 503 | // 1) Parameters, where we only know the exact stack slot after |
| 504 | // doing full register allocation. |
| 505 | // 2) Unallocated location. |
| 506 | DCHECK(output_.IsStackSlot() || output_.IsDoubleStackSlot() || output_.IsUnallocated()); |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 507 | output_ = location; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | void AddTemp(Location location) { |
| 511 | temps_.Add(location); |
| 512 | } |
| 513 | |
| 514 | Location GetTemp(uint32_t at) const { |
| 515 | return temps_.Get(at); |
| 516 | } |
| 517 | |
| 518 | void SetTempAt(uint32_t at, Location location) { |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 519 | DCHECK(temps_.Get(at).IsUnallocated() || temps_.Get(at).IsInvalid()); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 520 | temps_.Put(at, location); |
| 521 | } |
| 522 | |
| 523 | size_t GetTempCount() const { |
| 524 | return temps_.Size(); |
| 525 | } |
| 526 | |
Nicolas Geoffray | 94015b9 | 2015-06-04 18:21:04 +0100 | [diff] [blame] | 527 | bool HasTemps() const { return !temps_.IsEmpty(); } |
| 528 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 529 | Location Out() const { return output_; } |
| 530 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 531 | bool CanCall() const { return call_kind_ != kNoCall; } |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 532 | bool WillCall() const { return call_kind_ == kCall; } |
| 533 | bool OnlyCallsOnSlowPath() const { return call_kind_ == kCallOnSlowPath; } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 534 | bool NeedsSafepoint() const { return CanCall(); } |
| 535 | |
| 536 | void SetStackBit(uint32_t index) { |
| 537 | stack_mask_->SetBit(index); |
| 538 | } |
| 539 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 540 | void ClearStackBit(uint32_t index) { |
| 541 | stack_mask_->ClearBit(index); |
| 542 | } |
| 543 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 544 | void SetRegisterBit(uint32_t reg_id) { |
| 545 | register_mask_ |= (1 << reg_id); |
| 546 | } |
| 547 | |
Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 548 | uint32_t GetRegisterMask() const { |
| 549 | return register_mask_; |
| 550 | } |
| 551 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 552 | bool RegisterContainsObject(uint32_t reg_id) { |
| 553 | return RegisterSet::Contains(register_mask_, reg_id); |
| 554 | } |
| 555 | |
| 556 | void AddLiveRegister(Location location) { |
| 557 | live_registers_.Add(location); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | BitVector* GetStackMask() const { |
| 561 | return stack_mask_; |
| 562 | } |
| 563 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 564 | RegisterSet* GetLiveRegisters() { |
| 565 | return &live_registers_; |
| 566 | } |
| 567 | |
Nicolas Geoffray | 87d0376 | 2014-11-19 15:17:56 +0000 | [diff] [blame] | 568 | size_t GetNumberOfLiveRegisters() const { |
| 569 | return live_registers_.GetNumberOfRegisters(); |
| 570 | } |
| 571 | |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 572 | bool OutputUsesSameAs(uint32_t input_index) const { |
| 573 | return (input_index == 0) |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 574 | && output_.IsUnallocated() |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 575 | && (output_.GetPolicy() == Location::kSameAsFirstInput); |
| 576 | } |
| 577 | |
| 578 | bool IsFixedInput(uint32_t input_index) const { |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 579 | Location input = inputs_.Get(input_index); |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 580 | return input.IsRegister() |
Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 581 | || input.IsFpuRegister() |
| 582 | || input.IsPair() |
| 583 | || input.IsStackSlot() |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 584 | || input.IsDoubleStackSlot(); |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 587 | bool OutputCanOverlapWithInputs() const { |
Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 588 | return output_overlaps_ == Location::kOutputOverlap; |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 589 | } |
| 590 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 591 | bool Intrinsified() const { |
| 592 | return intrinsified_; |
| 593 | } |
| 594 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 595 | private: |
| 596 | GrowableArray<Location> inputs_; |
| 597 | GrowableArray<Location> temps_; |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 598 | // Whether the output overlaps with any of the inputs. If it overlaps, then it cannot |
| 599 | // share the same register as the inputs. |
Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 600 | Location::OutputOverlap output_overlaps_; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 601 | Location output_; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 602 | const CallKind call_kind_; |
| 603 | |
| 604 | // Mask of objects that live in the stack. |
| 605 | BitVector* stack_mask_; |
| 606 | |
| 607 | // Mask of objects that live in register. |
| 608 | uint32_t register_mask_; |
| 609 | |
| 610 | // Registers that are in use at this position. |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 611 | RegisterSet live_registers_; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 612 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 613 | // Whether these are locations for an intrinsified call. |
| 614 | const bool intrinsified_; |
| 615 | |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 616 | ART_FRIEND_TEST(RegisterAllocatorTest, ExpectedInRegisterHint); |
| 617 | ART_FRIEND_TEST(RegisterAllocatorTest, SameAsFirstInputHint); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 618 | DISALLOW_COPY_AND_ASSIGN(LocationSummary); |
| 619 | }; |
| 620 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 621 | } // namespace art |
| 622 | |
| 623 | #endif // ART_COMPILER_OPTIMIZING_LOCATIONS_H_ |