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