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