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" |
| 21 | #include "utils/allocation.h" |
| 22 | #include "utils/growable_array.h" |
| 23 | #include "utils/managed_register.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | class HInstruction; |
| 28 | |
| 29 | /** |
| 30 | * A Location is an abstraction over the potential location |
| 31 | * of an instruction. It could be in register or stack. |
| 32 | */ |
| 33 | class Location : public ValueObject { |
| 34 | public: |
| 35 | enum Kind { |
| 36 | kInvalid = 0, |
| 37 | kStackSlot = 1, // Word size slot. |
| 38 | kDoubleStackSlot = 2, // 64bit stack slot. |
| 39 | kRegister = 3, |
| 40 | // On 32bits architectures, quick can pass a long where the |
| 41 | // low bits are in the last parameter register, and the high |
| 42 | // bits are in a stack slot. The kQuickParameter kind is for |
| 43 | // handling this special case. |
| 44 | kQuickParameter = 4, |
| 45 | |
| 46 | // Unallocated location represents a location that is not fixed and can be |
| 47 | // allocated by a register allocator. Each unallocated location has |
| 48 | // a policy that specifies what kind of location is suitable. Payload |
| 49 | // contains register allocation policy. |
| 50 | kUnallocated = 5, |
| 51 | }; |
| 52 | |
| 53 | Location() : value_(kInvalid) { |
| 54 | DCHECK(!IsValid()); |
| 55 | } |
| 56 | |
| 57 | Location(const Location& other) : ValueObject(), value_(other.value_) {} |
| 58 | |
| 59 | Location& operator=(const Location& other) { |
| 60 | value_ = other.value_; |
| 61 | return *this; |
| 62 | } |
| 63 | |
| 64 | bool IsValid() const { |
| 65 | return value_ != kInvalid; |
| 66 | } |
| 67 | |
| 68 | bool IsInvalid() const { |
| 69 | return !IsValid(); |
| 70 | } |
| 71 | |
| 72 | bool IsConstant() const { |
| 73 | // TODO: support constants. |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | // Empty location. Used if there the location should be ignored. |
| 78 | static Location NoLocation() { |
| 79 | return Location(); |
| 80 | } |
| 81 | |
| 82 | // Register locations. |
| 83 | static Location RegisterLocation(ManagedRegister reg) { |
| 84 | return Location(kRegister, reg.RegId()); |
| 85 | } |
| 86 | |
| 87 | bool IsRegister() const { |
| 88 | return GetKind() == kRegister; |
| 89 | } |
| 90 | |
| 91 | ManagedRegister reg() const { |
| 92 | DCHECK(IsRegister()); |
| 93 | return static_cast<ManagedRegister>(GetPayload()); |
| 94 | } |
| 95 | |
| 96 | static uword EncodeStackIndex(intptr_t stack_index) { |
| 97 | DCHECK(-kStackIndexBias <= stack_index); |
| 98 | DCHECK(stack_index < kStackIndexBias); |
| 99 | return static_cast<uword>(kStackIndexBias + stack_index); |
| 100 | } |
| 101 | |
| 102 | static Location StackSlot(intptr_t stack_index) { |
| 103 | uword payload = EncodeStackIndex(stack_index); |
| 104 | Location loc(kStackSlot, payload); |
| 105 | // Ensure that sign is preserved. |
| 106 | DCHECK_EQ(loc.GetStackIndex(), stack_index); |
| 107 | return loc; |
| 108 | } |
| 109 | |
| 110 | bool IsStackSlot() const { |
| 111 | return GetKind() == kStackSlot; |
| 112 | } |
| 113 | |
| 114 | static Location DoubleStackSlot(intptr_t stack_index) { |
| 115 | uword payload = EncodeStackIndex(stack_index); |
| 116 | Location loc(kDoubleStackSlot, payload); |
| 117 | // Ensure that sign is preserved. |
| 118 | DCHECK_EQ(loc.GetStackIndex(), stack_index); |
| 119 | return loc; |
| 120 | } |
| 121 | |
| 122 | bool IsDoubleStackSlot() const { |
| 123 | return GetKind() == kDoubleStackSlot; |
| 124 | } |
| 125 | |
| 126 | intptr_t GetStackIndex() const { |
| 127 | DCHECK(IsStackSlot() || IsDoubleStackSlot()); |
| 128 | // Decode stack index manually to preserve sign. |
| 129 | return GetPayload() - kStackIndexBias; |
| 130 | } |
| 131 | |
| 132 | intptr_t GetHighStackIndex(uintptr_t word_size) const { |
| 133 | DCHECK(IsDoubleStackSlot()); |
| 134 | // Decode stack index manually to preserve sign. |
| 135 | return GetPayload() - kStackIndexBias + word_size; |
| 136 | } |
| 137 | |
| 138 | static Location QuickParameter(uint32_t parameter_index) { |
| 139 | return Location(kQuickParameter, parameter_index); |
| 140 | } |
| 141 | |
| 142 | uint32_t GetQuickParameterIndex() const { |
| 143 | DCHECK(IsQuickParameter()); |
| 144 | return GetPayload(); |
| 145 | } |
| 146 | |
| 147 | bool IsQuickParameter() const { |
| 148 | return GetKind() == kQuickParameter; |
| 149 | } |
| 150 | |
| 151 | arm::ArmManagedRegister AsArm() const; |
| 152 | x86::X86ManagedRegister AsX86() const; |
| 153 | |
| 154 | Kind GetKind() const { |
| 155 | return KindField::Decode(value_); |
| 156 | } |
| 157 | |
| 158 | bool Equals(Location other) const { |
| 159 | return value_ == other.value_; |
| 160 | } |
| 161 | |
| 162 | const char* DebugString() const { |
| 163 | switch (GetKind()) { |
| 164 | case kInvalid: return "?"; |
| 165 | case kRegister: return "R"; |
| 166 | case kStackSlot: return "S"; |
| 167 | case kDoubleStackSlot: return "DS"; |
| 168 | case kQuickParameter: return "Q"; |
| 169 | case kUnallocated: return "U"; |
| 170 | } |
| 171 | return "?"; |
| 172 | } |
| 173 | |
| 174 | // Unallocated locations. |
| 175 | enum Policy { |
| 176 | kAny, |
| 177 | kRequiresRegister, |
| 178 | kSameAsFirstInput, |
| 179 | }; |
| 180 | |
| 181 | bool IsUnallocated() const { |
| 182 | return GetKind() == kUnallocated; |
| 183 | } |
| 184 | |
| 185 | static Location UnallocatedLocation(Policy policy) { |
| 186 | return Location(kUnallocated, PolicyField::Encode(policy)); |
| 187 | } |
| 188 | |
| 189 | // Any free register is suitable to replace this unallocated location. |
| 190 | static Location Any() { |
| 191 | return UnallocatedLocation(kAny); |
| 192 | } |
| 193 | |
| 194 | static Location RequiresRegister() { |
| 195 | return UnallocatedLocation(kRequiresRegister); |
| 196 | } |
| 197 | |
| 198 | // The location of the first input to the instruction will be |
| 199 | // used to replace this unallocated location. |
| 200 | static Location SameAsFirstInput() { |
| 201 | return UnallocatedLocation(kSameAsFirstInput); |
| 202 | } |
| 203 | |
| 204 | Policy GetPolicy() const { |
| 205 | DCHECK(IsUnallocated()); |
| 206 | return PolicyField::Decode(GetPayload()); |
| 207 | } |
| 208 | |
| 209 | uword GetEncoding() const { |
| 210 | return GetPayload(); |
| 211 | } |
| 212 | |
| 213 | private: |
| 214 | // Number of bits required to encode Kind value. |
| 215 | static constexpr uint32_t kBitsForKind = 4; |
| 216 | static constexpr uint32_t kBitsForPayload = kWordSize * kBitsPerByte - kBitsForKind; |
| 217 | |
| 218 | explicit Location(uword value) : value_(value) {} |
| 219 | |
| 220 | Location(Kind kind, uword payload) |
| 221 | : value_(KindField::Encode(kind) | PayloadField::Encode(payload)) {} |
| 222 | |
| 223 | uword GetPayload() const { |
| 224 | return PayloadField::Decode(value_); |
| 225 | } |
| 226 | |
| 227 | typedef BitField<Kind, 0, kBitsForKind> KindField; |
| 228 | typedef BitField<uword, kBitsForKind, kBitsForPayload> PayloadField; |
| 229 | |
| 230 | // Layout for kUnallocated locations payload. |
| 231 | typedef BitField<Policy, 0, 3> PolicyField; |
| 232 | |
| 233 | // Layout for stack slots. |
| 234 | static const intptr_t kStackIndexBias = |
| 235 | static_cast<intptr_t>(1) << (kBitsForPayload - 1); |
| 236 | |
| 237 | // Location either contains kind and payload fields or a tagged handle for |
| 238 | // a constant locations. Values of enumeration Kind are selected in such a |
| 239 | // way that none of them can be interpreted as a kConstant tag. |
| 240 | uword value_; |
| 241 | }; |
| 242 | |
| 243 | /** |
| 244 | * The code generator computes LocationSummary for each instruction so that |
| 245 | * the instruction itself knows what code to generate: where to find the inputs |
| 246 | * and where to place the result. |
| 247 | * |
| 248 | * The intent is to have the code for generating the instruction independent of |
| 249 | * register allocation. A register allocator just has to provide a LocationSummary. |
| 250 | */ |
| 251 | class LocationSummary : public ArenaObject { |
| 252 | public: |
| 253 | explicit LocationSummary(HInstruction* instruction); |
| 254 | |
| 255 | void SetInAt(uint32_t at, Location location) { |
| 256 | inputs_.Put(at, location); |
| 257 | } |
| 258 | |
| 259 | Location InAt(uint32_t at) const { |
| 260 | return inputs_.Get(at); |
| 261 | } |
| 262 | |
| 263 | size_t GetInputCount() const { |
| 264 | return inputs_.Size(); |
| 265 | } |
| 266 | |
| 267 | void SetOut(Location location) { |
| 268 | output_ = Location(location); |
| 269 | } |
| 270 | |
| 271 | void AddTemp(Location location) { |
| 272 | temps_.Add(location); |
| 273 | } |
| 274 | |
| 275 | Location GetTemp(uint32_t at) const { |
| 276 | return temps_.Get(at); |
| 277 | } |
| 278 | |
| 279 | void SetTempAt(uint32_t at, Location location) { |
| 280 | temps_.Put(at, location); |
| 281 | } |
| 282 | |
| 283 | size_t GetTempCount() const { |
| 284 | return temps_.Size(); |
| 285 | } |
| 286 | |
| 287 | Location Out() const { return output_; } |
| 288 | |
| 289 | private: |
| 290 | GrowableArray<Location> inputs_; |
| 291 | GrowableArray<Location> temps_; |
| 292 | Location output_; |
| 293 | |
| 294 | DISALLOW_COPY_AND_ASSIGN(LocationSummary); |
| 295 | }; |
| 296 | |
| 297 | } // namespace art |
| 298 | |
| 299 | #endif // ART_COMPILER_OPTIMIZING_LOCATIONS_H_ |