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