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