| 1 | //===--- InfoByHwMode.h -----------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // Classes that implement data parameterized by HW modes for instruction |
| 9 | // selection. Currently it is ValueTypeByHwMode (parameterized ValueType), |
| 10 | // and RegSizeInfoByHwMode (parameterized register/spill size and alignment |
| 11 | // data). |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_UTILS_TABLEGEN_COMMON_INFOBYHWMODE_H |
| 15 | #define LLVM_UTILS_TABLEGEN_COMMON_INFOBYHWMODE_H |
| 16 | |
| 17 | #include "CodeGenHwModes.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/SortedVectorMap.h" |
| 20 | #include "llvm/ADT/StringRef.h" |
| 21 | #include "llvm/CodeGenTypes/MachineValueType.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | #include <cassert> |
| 24 | #include <limits> |
| 25 | #include <map> |
| 26 | #include <string> |
| 27 | #include <tuple> |
| 28 | |
| 29 | namespace llvm { |
| 30 | |
| 31 | class CodeGenRegBank; |
| 32 | class CodeGenRegister; |
| 33 | class CodeGenRegisterClass; |
| 34 | class Record; |
| 35 | class raw_ostream; |
| 36 | |
| 37 | template <typename InfoT, typename MapT = std::map<unsigned, InfoT>> |
| 38 | struct InfoByHwMode; |
| 39 | |
| 40 | std::string getModeName(unsigned Mode); |
| 41 | |
| 42 | enum : unsigned { |
| 43 | DefaultMode = CodeGenHwModes::DefaultMode, |
| 44 | }; |
| 45 | |
| 46 | template <typename InfoByHwModeT> |
| 47 | void union_modes(const InfoByHwModeT &A, const InfoByHwModeT &B, |
| 48 | SmallVectorImpl<unsigned> &Modes) { |
| 49 | auto AI = A.begin(); |
| 50 | auto BI = B.begin(); |
| 51 | |
| 52 | // Skip default mode, but remember if we had one. |
| 53 | bool HasDefault = false; |
| 54 | if (AI != A.end() && AI->first == DefaultMode) { |
| 55 | HasDefault = true; |
| 56 | ++AI; |
| 57 | } |
| 58 | if (BI != B.end() && BI->first == DefaultMode) { |
| 59 | HasDefault = true; |
| 60 | ++BI; |
| 61 | } |
| 62 | |
| 63 | while (AI != A.end()) { |
| 64 | // If we're done with B, finish A. |
| 65 | if (BI == B.end()) { |
| 66 | for (; AI != A.end(); ++AI) |
| 67 | Modes.push_back(Elt: AI->first); |
| 68 | break; |
| 69 | } |
| 70 | |
| 71 | if (BI->first < AI->first) { |
| 72 | Modes.push_back(Elt: BI->first); |
| 73 | ++BI; |
| 74 | } else { |
| 75 | Modes.push_back(Elt: AI->first); |
| 76 | if (AI->first == BI->first) |
| 77 | ++BI; |
| 78 | ++AI; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Finish B. |
| 83 | for (; BI != B.end(); ++BI) |
| 84 | Modes.push_back(Elt: BI->first); |
| 85 | |
| 86 | // Make sure that the default mode is last on the list. |
| 87 | if (HasDefault) |
| 88 | Modes.push_back(Elt: DefaultMode); |
| 89 | } |
| 90 | |
| 91 | template <typename InfoT, typename MapT> struct InfoByHwMode { |
| 92 | using MapType = MapT; |
| 93 | using PairType = typename MapType::value_type; |
| 94 | using iterator = typename MapType::iterator; |
| 95 | using const_iterator = typename MapType::const_iterator; |
| 96 | |
| 97 | explicit InfoByHwMode(const Record *Def = nullptr) : Def(Def) {}; |
| 98 | InfoByHwMode(const MapType &M) : Map(M) {} |
| 99 | |
| 100 | LLVM_ATTRIBUTE_ALWAYS_INLINE |
| 101 | iterator begin() { return Map.begin(); } |
| 102 | LLVM_ATTRIBUTE_ALWAYS_INLINE |
| 103 | iterator end() { return Map.end(); } |
| 104 | LLVM_ATTRIBUTE_ALWAYS_INLINE |
| 105 | const_iterator begin() const { return Map.begin(); } |
| 106 | LLVM_ATTRIBUTE_ALWAYS_INLINE |
| 107 | const_iterator end() const { return Map.end(); } |
| 108 | LLVM_ATTRIBUTE_ALWAYS_INLINE |
| 109 | size_t size() const { return Map.size(); } |
| 110 | LLVM_ATTRIBUTE_ALWAYS_INLINE |
| 111 | bool empty() const { return Map.empty(); } |
| 112 | |
| 113 | LLVM_ATTRIBUTE_ALWAYS_INLINE |
| 114 | bool hasMode(unsigned M) const { return Map.find(M) != Map.end(); } |
| 115 | LLVM_ATTRIBUTE_ALWAYS_INLINE |
| 116 | bool hasDefault() const { |
| 117 | return !Map.empty() && Map.begin()->first == DefaultMode; |
| 118 | } |
| 119 | |
| 120 | InfoT &get(unsigned Mode) { |
| 121 | auto F = Map.find(Mode); |
| 122 | if (F != Map.end()) |
| 123 | return F->second; |
| 124 | |
| 125 | // Copy and insert the default mode which should be first. |
| 126 | assert(hasDefault()); |
| 127 | auto P = Map.try_emplace(Mode, Map.begin()->second); |
| 128 | return P.first->second; |
| 129 | } |
| 130 | const InfoT &get(unsigned Mode) const { |
| 131 | auto F = Map.find(Mode); |
| 132 | if (F != Map.end()) |
| 133 | return F->second; |
| 134 | // Get the default mode which should be first. |
| 135 | F = Map.begin(); |
| 136 | assert(F != Map.end() && F->first == DefaultMode); |
| 137 | return F->second; |
| 138 | } |
| 139 | |
| 140 | LLVM_ATTRIBUTE_ALWAYS_INLINE |
| 141 | bool isSimple() const { |
| 142 | return Map.size() == 1 && Map.begin()->first == DefaultMode; |
| 143 | } |
| 144 | LLVM_ATTRIBUTE_ALWAYS_INLINE |
| 145 | const InfoT &getSimple() const { |
| 146 | assert(isSimple()); |
| 147 | return Map.begin()->second; |
| 148 | } |
| 149 | |
| 150 | const Record *getRecord() const { return Def; } |
| 151 | |
| 152 | protected: |
| 153 | MapType Map; |
| 154 | const Record *Def; |
| 155 | }; |
| 156 | |
| 157 | struct ValueTypeByHwMode : public InfoByHwMode<MVT> { |
| 158 | ValueTypeByHwMode(const Record *R, const CodeGenHwModes &CGH); |
| 159 | ValueTypeByHwMode(const Record *R, MVT T); |
| 160 | ValueTypeByHwMode(MVT T) { Map.try_emplace(k: DefaultMode, args&: T); } |
| 161 | ValueTypeByHwMode() = default; |
| 162 | |
| 163 | bool operator==(const ValueTypeByHwMode &T) const; |
| 164 | bool operator<(const ValueTypeByHwMode &T) const; |
| 165 | |
| 166 | bool isValid() const { return !Map.empty(); } |
| 167 | MVT getType(unsigned Mode) const { return get(Mode); } |
| 168 | void insertTypeForMode(unsigned Mode, MVT Type) { |
| 169 | Map.try_emplace(k: Mode, args&: Type); |
| 170 | } |
| 171 | |
| 172 | static StringRef getMVTName(MVT T); |
| 173 | void writeToStream(raw_ostream &OS) const; |
| 174 | void dump() const; |
| 175 | |
| 176 | unsigned PtrAddrSpace = std::numeric_limits<unsigned>::max(); |
| 177 | bool isPointer() const { |
| 178 | return PtrAddrSpace != std::numeric_limits<unsigned>::max(); |
| 179 | } |
| 180 | }; |
| 181 | |
| 182 | ValueTypeByHwMode getValueTypeByHwMode(const Record *Rec, |
| 183 | const CodeGenHwModes &CGH); |
| 184 | |
| 185 | raw_ostream &operator<<(raw_ostream &OS, const ValueTypeByHwMode &T); |
| 186 | |
| 187 | struct RegSizeInfo { |
| 188 | unsigned RegSize; |
| 189 | unsigned SpillSize; |
| 190 | unsigned SpillAlignment; |
| 191 | |
| 192 | RegSizeInfo(const Record *R); |
| 193 | RegSizeInfo() = default; |
| 194 | bool operator<(const RegSizeInfo &I) const; |
| 195 | bool operator==(const RegSizeInfo &I) const { |
| 196 | return std::tie(args: RegSize, args: SpillSize, args: SpillAlignment) == |
| 197 | std::tie(args: I.RegSize, args: I.SpillSize, args: I.SpillAlignment); |
| 198 | } |
| 199 | bool operator!=(const RegSizeInfo &I) const { return !(*this == I); } |
| 200 | |
| 201 | bool isSubClassOf(const RegSizeInfo &I) const; |
| 202 | void writeToStream(raw_ostream &OS) const; |
| 203 | }; |
| 204 | |
| 205 | struct RegSizeInfoByHwMode : public InfoByHwMode<RegSizeInfo> { |
| 206 | RegSizeInfoByHwMode(const Record *R, const CodeGenHwModes &CGH); |
| 207 | RegSizeInfoByHwMode() = default; |
| 208 | bool operator<(const RegSizeInfoByHwMode &VI) const; |
| 209 | bool operator==(const RegSizeInfoByHwMode &VI) const; |
| 210 | bool operator!=(const RegSizeInfoByHwMode &VI) const { |
| 211 | return !(*this == VI); |
| 212 | } |
| 213 | |
| 214 | bool isSubClassOf(const RegSizeInfoByHwMode &I) const; |
| 215 | bool hasStricterSpillThan(const RegSizeInfoByHwMode &I) const; |
| 216 | |
| 217 | void writeToStream(raw_ostream &OS) const; |
| 218 | |
| 219 | void insertRegSizeForMode(unsigned Mode, RegSizeInfo Info) { |
| 220 | Map.try_emplace(k: Mode, args&: Info); |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | raw_ostream &operator<<(raw_ostream &OS, const RegSizeInfo &T); |
| 225 | raw_ostream &operator<<(raw_ostream &OS, const RegSizeInfoByHwMode &T); |
| 226 | |
| 227 | struct SubRegRange { |
| 228 | uint32_t Size; |
| 229 | uint32_t Offset; |
| 230 | |
| 231 | SubRegRange(const Record *R); |
| 232 | SubRegRange(uint32_t Size, uint32_t Offset) : Size(Size), Offset(Offset) {} |
| 233 | }; |
| 234 | |
| 235 | struct SubRegRangeByHwMode : public InfoByHwMode<SubRegRange> { |
| 236 | SubRegRangeByHwMode(const Record *R, const CodeGenHwModes &CGH); |
| 237 | SubRegRangeByHwMode(SubRegRange Range) { |
| 238 | Map.try_emplace(k: DefaultMode, args&: Range); |
| 239 | } |
| 240 | SubRegRangeByHwMode() = default; |
| 241 | |
| 242 | void insertSubRegRangeForMode(unsigned Mode, SubRegRange Info) { |
| 243 | Map.try_emplace(k: Mode, args&: Info); |
| 244 | } |
| 245 | }; |
| 246 | |
| 247 | struct EncodingInfoByHwMode : public InfoByHwMode<const Record *> { |
| 248 | EncodingInfoByHwMode(const Record *R, const CodeGenHwModes &CGH); |
| 249 | EncodingInfoByHwMode() = default; |
| 250 | }; |
| 251 | |
| 252 | struct RegClassByHwMode : public InfoByHwMode<const CodeGenRegisterClass *> { |
| 253 | public: |
| 254 | RegClassByHwMode(const Record *R, const CodeGenRegBank &RegBank); |
| 255 | RegClassByHwMode() = default; |
| 256 | }; |
| 257 | |
| 258 | struct RegisterByHwMode : public InfoByHwMode<const CodeGenRegister *> { |
| 259 | RegisterByHwMode(const Record *R, CodeGenRegBank &RegBank); |
| 260 | RegisterByHwMode() = default; |
| 261 | /// Resolve the register by calling <target>::RegByHwMode::get<name>(HwMode). |
| 262 | void emitResolverCall(raw_ostream &OS, const Twine &HwMode) const; |
| 263 | |
| 264 | private: |
| 265 | StringRef Namespace; |
| 266 | }; |
| 267 | |
| 268 | } // namespace llvm |
| 269 | |
| 270 | #endif // LLVM_UTILS_TABLEGEN_COMMON_INFOBYHWMODE_H |
| 271 | |