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