Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_COMPILER_OPTIMIZING_LOCATIONS_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_LOCATIONS_H_ |
| 19 | |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 20 | #include "base/arena_containers.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 21 | #include "base/arena_object.h" |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 22 | #include "base/bit_field.h" |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 23 | #include "base/bit_utils.h" |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 24 | #include "base/bit_vector.h" |
Vladimír Marko | 434d968 | 2022-11-04 14:04:17 +0000 | [diff] [blame] | 25 | #include "base/macros.h" |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 26 | #include "base/value_object.h" |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 27 | |
Vladimír Marko | 434d968 | 2022-11-04 14:04:17 +0000 | [diff] [blame] | 28 | namespace art HIDDEN { |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 29 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 30 | class HConstant; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 31 | class HInstruction; |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 32 | class Location; |
| 33 | |
| 34 | std::ostream& operator<<(std::ostream& os, const Location& location); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * A Location is an abstraction over the potential location |
| 38 | * of an instruction. It could be in register or stack. |
| 39 | */ |
Vladimir Marko | 76c92ac | 2015-09-17 15:39:16 +0100 | [diff] [blame] | 40 | class Location : public ValueObject { |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 41 | public: |
Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 42 | enum OutputOverlap { |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 43 | // The liveness of the output overlaps the liveness of one or |
| 44 | // several input(s); the register allocator cannot reuse an |
| 45 | // input's location for the output's location. |
Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 46 | kOutputOverlap, |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 47 | // The liveness of the output does not overlap the liveness of any |
| 48 | // input; the register allocator is allowed to reuse an input's |
| 49 | // location for the output's location. |
Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 50 | kNoOutputOverlap |
| 51 | }; |
Nicolas Geoffray | 9ae0daa | 2014-09-30 22:40:23 +0100 | [diff] [blame] | 52 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 53 | enum Kind { |
| 54 | kInvalid = 0, |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 55 | kConstant = 1, |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 56 | kStackSlot = 2, // 32bit stack slot. |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 57 | kDoubleStackSlot = 3, // 64bit stack slot. |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 58 | |
| 59 | kRegister = 4, // Core register. |
| 60 | |
| 61 | // We do not use the value 5 because it conflicts with kLocationConstantMask. |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 62 | kDoNotUse5 = 5, |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 63 | |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 64 | kFpuRegister = 6, // Float register. |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 65 | |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 66 | kRegisterPair = 7, // Long register. |
| 67 | |
| 68 | kFpuRegisterPair = 8, // Double register. |
| 69 | |
| 70 | // We do not use the value 9 because it conflicts with kLocationConstantMask. |
| 71 | kDoNotUse9 = 9, |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 72 | |
Aart Bik | 5576f37 | 2017-03-23 16:17:37 -0700 | [diff] [blame] | 73 | kSIMDStackSlot = 10, // 128bit stack slot. TODO: generalize with encoded #bytes? |
| 74 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 75 | // Unallocated location represents a location that is not fixed and can be |
| 76 | // allocated by a register allocator. Each unallocated location has |
| 77 | // a policy that specifies what kind of location is suitable. Payload |
| 78 | // contains register allocation policy. |
Aart Bik | 5576f37 | 2017-03-23 16:17:37 -0700 | [diff] [blame] | 79 | kUnallocated = 11, |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 80 | }; |
| 81 | |
Vladimir Marko | af06af4 | 2023-10-31 10:02:46 +0000 | [diff] [blame] | 82 | constexpr Location() : ValueObject(), value_(kInvalid) { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 83 | // Verify that non-constant location kinds do not interfere with kConstant. |
Andreas Gampe | 785d2f2 | 2014-11-03 22:57:30 -0800 | [diff] [blame] | 84 | static_assert((kInvalid & kLocationConstantMask) != kConstant, "TagError"); |
| 85 | static_assert((kUnallocated & kLocationConstantMask) != kConstant, "TagError"); |
| 86 | static_assert((kStackSlot & kLocationConstantMask) != kConstant, "TagError"); |
| 87 | static_assert((kDoubleStackSlot & kLocationConstantMask) != kConstant, "TagError"); |
Aart Bik | 5576f37 | 2017-03-23 16:17:37 -0700 | [diff] [blame] | 88 | static_assert((kSIMDStackSlot & kLocationConstantMask) != kConstant, "TagError"); |
Andreas Gampe | 785d2f2 | 2014-11-03 22:57:30 -0800 | [diff] [blame] | 89 | static_assert((kRegister & kLocationConstantMask) != kConstant, "TagError"); |
Andreas Gampe | 785d2f2 | 2014-11-03 22:57:30 -0800 | [diff] [blame] | 90 | static_assert((kFpuRegister & kLocationConstantMask) != kConstant, "TagError"); |
| 91 | static_assert((kRegisterPair & kLocationConstantMask) != kConstant, "TagError"); |
| 92 | static_assert((kFpuRegisterPair & kLocationConstantMask) != kConstant, "TagError"); |
| 93 | static_assert((kConstant & kLocationConstantMask) == kConstant, "TagError"); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 94 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 95 | DCHECK(!IsValid()); |
| 96 | } |
| 97 | |
Vladimir Marko | af06af4 | 2023-10-31 10:02:46 +0000 | [diff] [blame] | 98 | constexpr Location(const Location& other) = default; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 99 | |
Andreas Gampe | 2e6f38a | 2016-11-03 14:06:20 -0700 | [diff] [blame] | 100 | Location& operator=(const Location& other) = default; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 101 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 102 | bool IsConstant() const { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 103 | return (value_ & kLocationConstantMask) == kConstant; |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Vladimir Marko | f76ca8c | 2023-04-05 15:24:41 +0000 | [diff] [blame] | 106 | static Location ConstantLocation(HInstruction* constant) { |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 107 | DCHECK(constant != nullptr); |
Vladimir Marko | f76ca8c | 2023-04-05 15:24:41 +0000 | [diff] [blame] | 108 | if (kIsDebugBuild) { |
| 109 | // Call out-of-line helper to avoid circular dependency with `nodes.h`. |
| 110 | DCheckInstructionIsConstant(constant); |
| 111 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 112 | return Location(kConstant | reinterpret_cast<uintptr_t>(constant)); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | HConstant* GetConstant() const { |
| 116 | DCHECK(IsConstant()); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 117 | return reinterpret_cast<HConstant*>(value_ & ~kLocationConstantMask); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 120 | bool IsValid() const { |
| 121 | return value_ != kInvalid; |
| 122 | } |
| 123 | |
| 124 | bool IsInvalid() const { |
| 125 | return !IsValid(); |
| 126 | } |
| 127 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 128 | // Empty location. Used if there the location should be ignored. |
Vladimir Marko | af06af4 | 2023-10-31 10:02:46 +0000 | [diff] [blame] | 129 | static constexpr Location NoLocation() { |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 130 | return Location(); |
| 131 | } |
| 132 | |
| 133 | // Register locations. |
Vladimir Marko | af06af4 | 2023-10-31 10:02:46 +0000 | [diff] [blame] | 134 | static constexpr Location RegisterLocation(int reg) { |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 135 | return Location(kRegister, reg); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Vladimir Marko | af06af4 | 2023-10-31 10:02:46 +0000 | [diff] [blame] | 138 | static constexpr Location FpuRegisterLocation(int reg) { |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 139 | return Location(kFpuRegister, reg); |
| 140 | } |
| 141 | |
Vladimir Marko | af06af4 | 2023-10-31 10:02:46 +0000 | [diff] [blame] | 142 | static constexpr Location RegisterPairLocation(int low, int high) { |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 143 | return Location(kRegisterPair, low << 16 | high); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Vladimir Marko | af06af4 | 2023-10-31 10:02:46 +0000 | [diff] [blame] | 146 | static constexpr Location FpuRegisterPairLocation(int low, int high) { |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 147 | return Location(kFpuRegisterPair, low << 16 | high); |
| 148 | } |
| 149 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 150 | bool IsRegister() const { |
| 151 | return GetKind() == kRegister; |
| 152 | } |
| 153 | |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 154 | bool IsFpuRegister() const { |
| 155 | return GetKind() == kFpuRegister; |
| 156 | } |
| 157 | |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 158 | bool IsRegisterPair() const { |
| 159 | return GetKind() == kRegisterPair; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 162 | bool IsFpuRegisterPair() const { |
| 163 | return GetKind() == kFpuRegisterPair; |
| 164 | } |
| 165 | |
Nicolas Geoffray | da02afe | 2015-02-11 02:29:42 +0000 | [diff] [blame] | 166 | bool IsRegisterKind() const { |
| 167 | return IsRegister() || IsFpuRegister() || IsRegisterPair() || IsFpuRegisterPair(); |
| 168 | } |
| 169 | |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 170 | int reg() const { |
| 171 | DCHECK(IsRegister() || IsFpuRegister()); |
| 172 | return GetPayload(); |
| 173 | } |
| 174 | |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 175 | int low() const { |
| 176 | DCHECK(IsPair()); |
| 177 | return GetPayload() >> 16; |
| 178 | } |
| 179 | |
| 180 | int high() const { |
| 181 | DCHECK(IsPair()); |
| 182 | return GetPayload() & 0xFFFF; |
| 183 | } |
| 184 | |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 185 | template <typename T> |
Roland Levillain | 271ab9c | 2014-11-27 15:23:57 +0000 | [diff] [blame] | 186 | T AsRegister() const { |
| 187 | DCHECK(IsRegister()); |
| 188 | return static_cast<T>(reg()); |
| 189 | } |
| 190 | |
| 191 | template <typename T> |
| 192 | T AsFpuRegister() const { |
| 193 | DCHECK(IsFpuRegister()); |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 194 | return static_cast<T>(reg()); |
| 195 | } |
| 196 | |
| 197 | template <typename T> |
| 198 | T AsRegisterPairLow() const { |
| 199 | DCHECK(IsRegisterPair()); |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 200 | return static_cast<T>(low()); |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | template <typename T> |
| 204 | T AsRegisterPairHigh() const { |
| 205 | DCHECK(IsRegisterPair()); |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 206 | return static_cast<T>(high()); |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 209 | template <typename T> |
| 210 | T AsFpuRegisterPairLow() const { |
| 211 | DCHECK(IsFpuRegisterPair()); |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 212 | return static_cast<T>(low()); |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | template <typename T> |
| 216 | T AsFpuRegisterPairHigh() const { |
| 217 | DCHECK(IsFpuRegisterPair()); |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 218 | return static_cast<T>(high()); |
| 219 | } |
| 220 | |
| 221 | bool IsPair() const { |
| 222 | return IsRegisterPair() || IsFpuRegisterPair(); |
| 223 | } |
| 224 | |
| 225 | Location ToLow() const { |
Nicolas Geoffray | 234d69d | 2015-03-09 10:28:50 +0000 | [diff] [blame] | 226 | if (IsRegisterPair()) { |
| 227 | return Location::RegisterLocation(low()); |
| 228 | } else if (IsFpuRegisterPair()) { |
| 229 | return Location::FpuRegisterLocation(low()); |
| 230 | } else { |
| 231 | DCHECK(IsDoubleStackSlot()); |
| 232 | return Location::StackSlot(GetStackIndex()); |
| 233 | } |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | Location ToHigh() const { |
Nicolas Geoffray | 234d69d | 2015-03-09 10:28:50 +0000 | [diff] [blame] | 237 | if (IsRegisterPair()) { |
| 238 | return Location::RegisterLocation(high()); |
| 239 | } else if (IsFpuRegisterPair()) { |
| 240 | return Location::FpuRegisterLocation(high()); |
| 241 | } else { |
| 242 | DCHECK(IsDoubleStackSlot()); |
| 243 | return Location::StackSlot(GetHighStackIndex(4)); |
| 244 | } |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 247 | static uintptr_t EncodeStackIndex(intptr_t stack_index) { |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 248 | DCHECK(-kStackIndexBias <= stack_index); |
| 249 | DCHECK(stack_index < kStackIndexBias); |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 250 | return static_cast<uintptr_t>(kStackIndexBias + stack_index); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static Location StackSlot(intptr_t stack_index) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 254 | uintptr_t payload = EncodeStackIndex(stack_index); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 255 | Location loc(kStackSlot, payload); |
| 256 | // Ensure that sign is preserved. |
| 257 | DCHECK_EQ(loc.GetStackIndex(), stack_index); |
| 258 | return loc; |
| 259 | } |
| 260 | |
| 261 | bool IsStackSlot() const { |
| 262 | return GetKind() == kStackSlot; |
| 263 | } |
| 264 | |
| 265 | static Location DoubleStackSlot(intptr_t stack_index) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 266 | uintptr_t payload = EncodeStackIndex(stack_index); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 267 | Location loc(kDoubleStackSlot, payload); |
| 268 | // Ensure that sign is preserved. |
| 269 | DCHECK_EQ(loc.GetStackIndex(), stack_index); |
| 270 | return loc; |
| 271 | } |
| 272 | |
| 273 | bool IsDoubleStackSlot() const { |
| 274 | return GetKind() == kDoubleStackSlot; |
| 275 | } |
| 276 | |
Aart Bik | 5576f37 | 2017-03-23 16:17:37 -0700 | [diff] [blame] | 277 | static Location SIMDStackSlot(intptr_t stack_index) { |
| 278 | uintptr_t payload = EncodeStackIndex(stack_index); |
| 279 | Location loc(kSIMDStackSlot, payload); |
| 280 | // Ensure that sign is preserved. |
| 281 | DCHECK_EQ(loc.GetStackIndex(), stack_index); |
| 282 | return loc; |
| 283 | } |
| 284 | |
| 285 | bool IsSIMDStackSlot() const { |
| 286 | return GetKind() == kSIMDStackSlot; |
| 287 | } |
| 288 | |
Artem Serov | c8150b5 | 2019-07-31 18:28:00 +0100 | [diff] [blame] | 289 | static Location StackSlotByNumOfSlots(size_t num_of_slots, int spill_slot) { |
| 290 | DCHECK_NE(num_of_slots, 0u); |
| 291 | switch (num_of_slots) { |
| 292 | case 1u: |
| 293 | return Location::StackSlot(spill_slot); |
| 294 | case 2u: |
| 295 | return Location::DoubleStackSlot(spill_slot); |
| 296 | default: |
| 297 | // Assume all other stack slot sizes correspond to SIMD slot size. |
| 298 | return Location::SIMDStackSlot(spill_slot); |
| 299 | } |
| 300 | } |
| 301 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 302 | intptr_t GetStackIndex() const { |
Aart Bik | 5576f37 | 2017-03-23 16:17:37 -0700 | [diff] [blame] | 303 | DCHECK(IsStackSlot() || IsDoubleStackSlot() || IsSIMDStackSlot()); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 304 | // Decode stack index manually to preserve sign. |
| 305 | return GetPayload() - kStackIndexBias; |
| 306 | } |
| 307 | |
| 308 | intptr_t GetHighStackIndex(uintptr_t word_size) const { |
| 309 | DCHECK(IsDoubleStackSlot()); |
| 310 | // Decode stack index manually to preserve sign. |
| 311 | return GetPayload() - kStackIndexBias + word_size; |
| 312 | } |
| 313 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 314 | Kind GetKind() const { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 315 | return IsConstant() ? kConstant : KindField::Decode(value_); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | bool Equals(Location other) const { |
| 319 | return value_ == other.value_; |
| 320 | } |
| 321 | |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 322 | bool Contains(Location other) const { |
| 323 | if (Equals(other)) { |
| 324 | return true; |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 325 | } else if (IsPair() || IsDoubleStackSlot()) { |
| 326 | return ToLow().Equals(other) || ToHigh().Equals(other); |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 327 | } |
| 328 | return false; |
| 329 | } |
| 330 | |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 331 | bool OverlapsWith(Location other) const { |
| 332 | // Only check the overlapping case that can happen with our register allocation algorithm. |
| 333 | bool overlap = Contains(other) || other.Contains(*this); |
| 334 | if (kIsDebugBuild && !overlap) { |
| 335 | // Note: These are also overlapping cases. But we are not able to handle them in |
| 336 | // ParallelMoveResolverWithSwap. Make sure that we do not meet such case with our compiler. |
| 337 | if ((IsPair() && other.IsPair()) || (IsDoubleStackSlot() && other.IsDoubleStackSlot())) { |
| 338 | DCHECK(!Contains(other.ToLow())); |
| 339 | DCHECK(!Contains(other.ToHigh())); |
| 340 | } |
| 341 | } |
| 342 | return overlap; |
| 343 | } |
| 344 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 345 | const char* DebugString() const { |
| 346 | switch (GetKind()) { |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 347 | case kInvalid: return "I"; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 348 | case kRegister: return "R"; |
| 349 | case kStackSlot: return "S"; |
| 350 | case kDoubleStackSlot: return "DS"; |
Aart Bik | 5576f37 | 2017-03-23 16:17:37 -0700 | [diff] [blame] | 351 | case kSIMDStackSlot: return "SIMD"; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 352 | case kUnallocated: return "U"; |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 353 | case kConstant: return "C"; |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 354 | case kFpuRegister: return "F"; |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 355 | case kRegisterPair: return "RP"; |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 356 | case kFpuRegisterPair: return "FP"; |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 357 | case kDoNotUse5: // fall-through |
| 358 | case kDoNotUse9: |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 359 | LOG(FATAL) << "Should not use this location kind"; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 360 | } |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 361 | UNREACHABLE(); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | // Unallocated locations. |
| 365 | enum Policy { |
| 366 | kAny, |
| 367 | kRequiresRegister, |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 368 | kRequiresFpuRegister, |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 369 | kSameAsFirstInput, |
| 370 | }; |
| 371 | |
| 372 | bool IsUnallocated() const { |
| 373 | return GetKind() == kUnallocated; |
| 374 | } |
| 375 | |
| 376 | static Location UnallocatedLocation(Policy policy) { |
| 377 | return Location(kUnallocated, PolicyField::Encode(policy)); |
| 378 | } |
| 379 | |
| 380 | // Any free register is suitable to replace this unallocated location. |
| 381 | static Location Any() { |
| 382 | return UnallocatedLocation(kAny); |
| 383 | } |
| 384 | |
| 385 | static Location RequiresRegister() { |
| 386 | return UnallocatedLocation(kRequiresRegister); |
| 387 | } |
| 388 | |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 389 | static Location RequiresFpuRegister() { |
| 390 | return UnallocatedLocation(kRequiresFpuRegister); |
| 391 | } |
| 392 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 393 | static Location RegisterOrConstant(HInstruction* instruction); |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 394 | static Location RegisterOrInt32Constant(HInstruction* instruction); |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 395 | static Location ByteRegisterOrConstant(int reg, HInstruction* instruction); |
Mark Mendell | ea5af68 | 2015-10-22 17:35:49 -0400 | [diff] [blame] | 396 | static Location FpuRegisterOrConstant(HInstruction* instruction); |
| 397 | static Location FpuRegisterOrInt32Constant(HInstruction* instruction); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 398 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 399 | // The location of the first input to the instruction will be |
| 400 | // used to replace this unallocated location. |
| 401 | static Location SameAsFirstInput() { |
| 402 | return UnallocatedLocation(kSameAsFirstInput); |
| 403 | } |
| 404 | |
| 405 | Policy GetPolicy() const { |
| 406 | DCHECK(IsUnallocated()); |
| 407 | return PolicyField::Decode(GetPayload()); |
| 408 | } |
| 409 | |
Matthew Gharrity | d9ffd0d | 2016-06-22 10:27:55 -0700 | [diff] [blame] | 410 | bool RequiresRegisterKind() const { |
| 411 | return GetPolicy() == kRequiresRegister || GetPolicy() == kRequiresFpuRegister; |
| 412 | } |
| 413 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 414 | uintptr_t GetEncoding() const { |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 415 | return GetPayload(); |
| 416 | } |
| 417 | |
| 418 | private: |
| 419 | // Number of bits required to encode Kind value. |
| 420 | static constexpr uint32_t kBitsForKind = 4; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 421 | static constexpr uint32_t kBitsForPayload = kBitsPerIntPtrT - kBitsForKind; |
| 422 | static constexpr uintptr_t kLocationConstantMask = 0x3; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 423 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 424 | explicit Location(uintptr_t value) : value_(value) {} |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 425 | |
Vladimir Marko | af06af4 | 2023-10-31 10:02:46 +0000 | [diff] [blame] | 426 | constexpr Location(Kind kind, uintptr_t payload) |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 427 | : value_(KindField::Encode(kind) | PayloadField::Encode(payload)) {} |
| 428 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 429 | uintptr_t GetPayload() const { |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 430 | return PayloadField::Decode(value_); |
| 431 | } |
| 432 | |
Vladimir Marko | f76ca8c | 2023-04-05 15:24:41 +0000 | [diff] [blame] | 433 | static void DCheckInstructionIsConstant(HInstruction* instruction); |
| 434 | |
Vladimir Marko | 4f99071 | 2021-07-14 12:45:13 +0100 | [diff] [blame] | 435 | using KindField = BitField<Kind, 0, kBitsForKind>; |
| 436 | using PayloadField = BitField<uintptr_t, kBitsForKind, kBitsForPayload>; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 437 | |
| 438 | // Layout for kUnallocated locations payload. |
Vladimir Marko | 4f99071 | 2021-07-14 12:45:13 +0100 | [diff] [blame] | 439 | using PolicyField = BitField<Policy, 0, 3>; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 440 | |
| 441 | // Layout for stack slots. |
| 442 | static const intptr_t kStackIndexBias = |
| 443 | static_cast<intptr_t>(1) << (kBitsForPayload - 1); |
| 444 | |
| 445 | // Location either contains kind and payload fields or a tagged handle for |
| 446 | // a constant locations. Values of enumeration Kind are selected in such a |
| 447 | // way that none of them can be interpreted as a kConstant tag. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 448 | uintptr_t value_; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 449 | }; |
Vladimir Marko | 9974e3c | 2020-06-10 16:27:06 +0100 | [diff] [blame] | 450 | std::ostream& operator<<(std::ostream& os, Location::Kind rhs); |
| 451 | std::ostream& operator<<(std::ostream& os, Location::Policy rhs); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 452 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 453 | class RegisterSet : public ValueObject { |
| 454 | public: |
Vladimir Marko | 804b03f | 2016-09-14 16:26:36 +0100 | [diff] [blame] | 455 | static RegisterSet Empty() { return RegisterSet(); } |
Aart Bik | b13c65b | 2017-03-21 20:14:07 -0700 | [diff] [blame] | 456 | static RegisterSet AllFpu() { return RegisterSet(0, -1); } |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 457 | |
| 458 | void Add(Location loc) { |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 459 | if (loc.IsRegister()) { |
| 460 | core_registers_ |= (1 << loc.reg()); |
| 461 | } else { |
| 462 | DCHECK(loc.IsFpuRegister()); |
| 463 | floating_point_registers_ |= (1 << loc.reg()); |
| 464 | } |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 465 | } |
| 466 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 467 | void Remove(Location loc) { |
| 468 | if (loc.IsRegister()) { |
| 469 | core_registers_ &= ~(1 << loc.reg()); |
| 470 | } else { |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 471 | DCHECK(loc.IsFpuRegister()) << loc; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 472 | floating_point_registers_ &= ~(1 << loc.reg()); |
| 473 | } |
| 474 | } |
| 475 | |
Nicolas Geoffray | 45b83af | 2015-07-06 15:12:53 +0000 | [diff] [blame] | 476 | bool ContainsCoreRegister(uint32_t id) const { |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 477 | return Contains(core_registers_, id); |
| 478 | } |
| 479 | |
Nicolas Geoffray | 45b83af | 2015-07-06 15:12:53 +0000 | [diff] [blame] | 480 | bool ContainsFloatingPointRegister(uint32_t id) const { |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 481 | return Contains(floating_point_registers_, id); |
| 482 | } |
| 483 | |
| 484 | static bool Contains(uint32_t register_set, uint32_t reg) { |
| 485 | return (register_set & (1 << reg)) != 0; |
| 486 | } |
| 487 | |
Andra Danciu | e3e187f | 2020-07-30 12:19:31 +0000 | [diff] [blame] | 488 | bool OverlapsRegisters(Location out) { |
| 489 | DCHECK(out.IsRegisterKind()); |
| 490 | switch (out.GetKind()) { |
| 491 | case Location::Kind::kRegister: |
| 492 | return ContainsCoreRegister(out.reg()); |
| 493 | case Location::Kind::kFpuRegister: |
| 494 | return ContainsFloatingPointRegister(out.reg()); |
| 495 | case Location::Kind::kRegisterPair: |
| 496 | return ContainsCoreRegister(out.low()) || ContainsCoreRegister(out.high()); |
| 497 | case Location::Kind::kFpuRegisterPair: |
| 498 | return ContainsFloatingPointRegister(out.low()) || |
| 499 | ContainsFloatingPointRegister(out.high()); |
| 500 | default: |
| 501 | return false; |
| 502 | } |
| 503 | } |
| 504 | |
Nicolas Geoffray | 87d0376 | 2014-11-19 15:17:56 +0000 | [diff] [blame] | 505 | size_t GetNumberOfRegisters() const { |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 506 | return POPCOUNT(core_registers_) + POPCOUNT(floating_point_registers_); |
Nicolas Geoffray | 87d0376 | 2014-11-19 15:17:56 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 509 | uint32_t GetCoreRegisters() const { |
| 510 | return core_registers_; |
| 511 | } |
| 512 | |
| 513 | uint32_t GetFloatingPointRegisters() const { |
| 514 | return floating_point_registers_; |
| 515 | } |
| 516 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 517 | private: |
Vladimir Marko | 804b03f | 2016-09-14 16:26:36 +0100 | [diff] [blame] | 518 | RegisterSet() : core_registers_(0), floating_point_registers_(0) {} |
Aart Bik | b13c65b | 2017-03-21 20:14:07 -0700 | [diff] [blame] | 519 | RegisterSet(uint32_t core, uint32_t fp) : core_registers_(core), floating_point_registers_(fp) {} |
Vladimir Marko | 804b03f | 2016-09-14 16:26:36 +0100 | [diff] [blame] | 520 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 521 | uint32_t core_registers_; |
| 522 | uint32_t floating_point_registers_; |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 523 | }; |
| 524 | |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 525 | static constexpr bool kIntrinsified = true; |
| 526 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 527 | /** |
| 528 | * The code generator computes LocationSummary for each instruction so that |
| 529 | * the instruction itself knows what code to generate: where to find the inputs |
| 530 | * and where to place the result. |
| 531 | * |
| 532 | * The intent is to have the code for generating the instruction independent of |
| 533 | * register allocation. A register allocator just has to provide a LocationSummary. |
| 534 | */ |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 535 | class LocationSummary : public ArenaObject<kArenaAllocLocationSummary> { |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 536 | public: |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 537 | enum CallKind { |
| 538 | kNoCall, |
Serban Constantinescu | 806f012 | 2016-03-09 11:10:16 +0000 | [diff] [blame] | 539 | kCallOnMainAndSlowPath, |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 540 | kCallOnSlowPath, |
Serban Constantinescu | 54ff482 | 2016-07-07 18:03:19 +0100 | [diff] [blame] | 541 | kCallOnMainOnly |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 542 | }; |
| 543 | |
Chih-Hung Hsieh | a593118 | 2016-09-01 15:08:13 -0700 | [diff] [blame] | 544 | explicit LocationSummary(HInstruction* instruction, |
| 545 | CallKind call_kind = kNoCall, |
| 546 | bool intrinsified = false); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 547 | |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 548 | void SetInAt(uint32_t at, Location location) { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 549 | inputs_[at] = location; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | Location InAt(uint32_t at) const { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 553 | return inputs_[at]; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | size_t GetInputCount() const { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 557 | return inputs_.size(); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 558 | } |
| 559 | |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 560 | // Set the output location. Argument `overlaps` tells whether the |
| 561 | // output overlaps any of the inputs (if so, it cannot share the |
| 562 | // same register as one of the inputs); it is set to |
| 563 | // `Location::kOutputOverlap` by default for safety. |
Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 564 | void SetOut(Location location, Location::OutputOverlap overlaps = Location::kOutputOverlap) { |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 565 | DCHECK(output_.IsInvalid()); |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 566 | output_overlaps_ = overlaps; |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 567 | output_ = location; |
| 568 | } |
| 569 | |
| 570 | void UpdateOut(Location location) { |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 571 | // There are two reasons for updating an output: |
| 572 | // 1) Parameters, where we only know the exact stack slot after |
| 573 | // doing full register allocation. |
| 574 | // 2) Unallocated location. |
| 575 | DCHECK(output_.IsStackSlot() || output_.IsDoubleStackSlot() || output_.IsUnallocated()); |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 576 | output_ = location; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | void AddTemp(Location location) { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 580 | temps_.push_back(location); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 581 | } |
| 582 | |
Mathieu Chartier | 5c44c1b | 2016-11-04 18:13:04 -0700 | [diff] [blame] | 583 | void AddRegisterTemps(size_t count) { |
| 584 | for (size_t i = 0; i < count; ++i) { |
| 585 | AddTemp(Location::RequiresRegister()); |
| 586 | } |
| 587 | } |
| 588 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 589 | Location GetTemp(uint32_t at) const { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 590 | return temps_[at]; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | void SetTempAt(uint32_t at, Location location) { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 594 | DCHECK(temps_[at].IsUnallocated() || temps_[at].IsInvalid()); |
| 595 | temps_[at] = location; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | size_t GetTempCount() const { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 599 | return temps_.size(); |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 600 | } |
| 601 | |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 602 | bool HasTemps() const { return !temps_.empty(); } |
Nicolas Geoffray | 94015b9 | 2015-06-04 18:21:04 +0100 | [diff] [blame] | 603 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 604 | Location Out() const { return output_; } |
| 605 | |
Serban Constantinescu | 806f012 | 2016-03-09 11:10:16 +0000 | [diff] [blame] | 606 | bool CanCall() const { |
| 607 | return call_kind_ != kNoCall; |
| 608 | } |
| 609 | |
| 610 | bool WillCall() const { |
| 611 | return call_kind_ == kCallOnMainOnly || call_kind_ == kCallOnMainAndSlowPath; |
| 612 | } |
| 613 | |
| 614 | bool CallsOnSlowPath() const { |
Santiago Aboy Solanes | e8a822d | 2021-09-13 14:40:53 +0100 | [diff] [blame] | 615 | return OnlyCallsOnSlowPath() || CallsOnMainAndSlowPath(); |
Serban Constantinescu | 806f012 | 2016-03-09 11:10:16 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | bool OnlyCallsOnSlowPath() const { |
| 619 | return call_kind_ == kCallOnSlowPath; |
| 620 | } |
| 621 | |
Santiago Aboy Solanes | e8a822d | 2021-09-13 14:40:53 +0100 | [diff] [blame] | 622 | bool NeedsSuspendCheckEntry() const { |
| 623 | // Slow path calls do not need a SuspendCheck at method entry since they go into the runtime, |
| 624 | // which we expect to either do a suspend check or return quickly. |
| 625 | return WillCall(); |
| 626 | } |
| 627 | |
Serban Constantinescu | 806f012 | 2016-03-09 11:10:16 +0000 | [diff] [blame] | 628 | bool CallsOnMainAndSlowPath() const { |
| 629 | return call_kind_ == kCallOnMainAndSlowPath; |
| 630 | } |
| 631 | |
| 632 | bool NeedsSafepoint() const { |
| 633 | return CanCall(); |
| 634 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 635 | |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 636 | void SetCustomSlowPathCallerSaves(const RegisterSet& caller_saves) { |
| 637 | DCHECK(OnlyCallsOnSlowPath()); |
| 638 | has_custom_slow_path_calling_convention_ = true; |
| 639 | custom_slow_path_caller_saves_ = caller_saves; |
| 640 | } |
| 641 | |
| 642 | bool HasCustomSlowPathCallingConvention() const { |
| 643 | return has_custom_slow_path_calling_convention_; |
| 644 | } |
| 645 | |
| 646 | const RegisterSet& GetCustomSlowPathCallerSaves() const { |
| 647 | DCHECK(HasCustomSlowPathCallingConvention()); |
| 648 | return custom_slow_path_caller_saves_; |
| 649 | } |
| 650 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 651 | void SetStackBit(uint32_t index) { |
| 652 | stack_mask_->SetBit(index); |
| 653 | } |
| 654 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 655 | void ClearStackBit(uint32_t index) { |
| 656 | stack_mask_->ClearBit(index); |
| 657 | } |
| 658 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 659 | void SetRegisterBit(uint32_t reg_id) { |
| 660 | register_mask_ |= (1 << reg_id); |
| 661 | } |
| 662 | |
Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 663 | uint32_t GetRegisterMask() const { |
| 664 | return register_mask_; |
| 665 | } |
| 666 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 667 | bool RegisterContainsObject(uint32_t reg_id) { |
| 668 | return RegisterSet::Contains(register_mask_, reg_id); |
| 669 | } |
| 670 | |
| 671 | void AddLiveRegister(Location location) { |
| 672 | live_registers_.Add(location); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | BitVector* GetStackMask() const { |
| 676 | return stack_mask_; |
| 677 | } |
| 678 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 679 | RegisterSet* GetLiveRegisters() { |
| 680 | return &live_registers_; |
| 681 | } |
| 682 | |
Nicolas Geoffray | 87d0376 | 2014-11-19 15:17:56 +0000 | [diff] [blame] | 683 | size_t GetNumberOfLiveRegisters() const { |
| 684 | return live_registers_.GetNumberOfRegisters(); |
| 685 | } |
| 686 | |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 687 | bool OutputUsesSameAs(uint32_t input_index) const { |
| 688 | return (input_index == 0) |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 689 | && output_.IsUnallocated() |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 690 | && (output_.GetPolicy() == Location::kSameAsFirstInput); |
| 691 | } |
| 692 | |
| 693 | bool IsFixedInput(uint32_t input_index) const { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 694 | Location input = inputs_[input_index]; |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 695 | return input.IsRegister() |
Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 696 | || input.IsFpuRegister() |
| 697 | || input.IsPair() |
| 698 | || input.IsStackSlot() |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 699 | || input.IsDoubleStackSlot(); |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 700 | } |
| 701 | |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 702 | bool OutputCanOverlapWithInputs() const { |
Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 703 | return output_overlaps_ == Location::kOutputOverlap; |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 704 | } |
| 705 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 706 | bool Intrinsified() const { |
| 707 | return intrinsified_; |
| 708 | } |
| 709 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 710 | private: |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 711 | LocationSummary(HInstruction* instruction, |
| 712 | CallKind call_kind, |
| 713 | bool intrinsified, |
| 714 | ArenaAllocator* allocator); |
| 715 | |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 716 | ArenaVector<Location> inputs_; |
| 717 | ArenaVector<Location> temps_; |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 718 | const CallKind call_kind_; |
| 719 | // Whether these are locations for an intrinsified call. |
| 720 | const bool intrinsified_; |
| 721 | // Whether the slow path has default or custom calling convention. |
| 722 | bool has_custom_slow_path_calling_convention_; |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 723 | // Whether the output overlaps with any of the inputs. If it overlaps, then it cannot |
| 724 | // share the same register as the inputs. |
Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 725 | Location::OutputOverlap output_overlaps_; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 726 | Location output_; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 727 | |
| 728 | // Mask of objects that live in the stack. |
| 729 | BitVector* stack_mask_; |
| 730 | |
| 731 | // Mask of objects that live in register. |
| 732 | uint32_t register_mask_; |
| 733 | |
| 734 | // Registers that are in use at this position. |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 735 | RegisterSet live_registers_; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 736 | |
Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 737 | // Custom slow path caller saves. Valid only if indicated by slow_path_calling_convention_. |
| 738 | RegisterSet custom_slow_path_caller_saves_; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 739 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 740 | friend class RegisterAllocatorTest; |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 741 | DISALLOW_COPY_AND_ASSIGN(LocationSummary); |
| 742 | }; |
| 743 | |
Nicolas Geoffray | 76716a6 | 2014-05-23 10:14:19 +0100 | [diff] [blame] | 744 | } // namespace art |
| 745 | |
| 746 | #endif // ART_COMPILER_OPTIMIZING_LOCATIONS_H_ |