Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame^] | 1 | //===--- InfoByHwMode.h -----------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // Classes that implement data parameterized by HW modes for instruction |
| 10 | // selection. Currently it is ValueTypeByHwMode (parameterized ValueType), |
| 11 | // and RegSizeInfoByHwMode (parameterized register/spill size and alignment |
| 12 | // data). |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_UTILS_TABLEGEN_INFOBYHWMODE_H |
| 16 | #define LLVM_UTILS_TABLEGEN_INFOBYHWMODE_H |
| 17 | |
| 18 | #include "CodeGenHwModes.h" |
| 19 | #include "llvm/CodeGen/MachineValueType.h" |
| 20 | |
| 21 | #include <map> |
| 22 | #include <set> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | namespace llvm { |
| 27 | |
| 28 | struct CodeGenHwModes; |
| 29 | class Record; |
| 30 | |
| 31 | template <typename InfoT> struct InfoByHwMode; |
| 32 | |
| 33 | std::string getModeName(unsigned Mode); |
| 34 | |
| 35 | enum : unsigned { |
| 36 | DefaultMode = CodeGenHwModes::DefaultMode, |
| 37 | }; |
| 38 | |
| 39 | template <typename InfoT> |
| 40 | std::vector<unsigned> union_modes(const InfoByHwMode<InfoT> &A, |
| 41 | const InfoByHwMode<InfoT> &B) { |
| 42 | std::vector<unsigned> V; |
| 43 | std::set<unsigned> U; |
| 44 | for (const auto &P : A) |
| 45 | U.insert(P.first); |
| 46 | for (const auto &P : B) |
| 47 | U.insert(P.first); |
| 48 | // Make sure that the default mode is last on the list. |
| 49 | bool HasDefault = U.count(DefaultMode); |
| 50 | for (unsigned M : U) |
| 51 | if (M != DefaultMode) |
| 52 | V.push_back(M); |
| 53 | if (HasDefault) |
| 54 | V.push_back(DefaultMode); |
| 55 | return V; |
| 56 | } |
| 57 | |
| 58 | template <typename InfoT> |
| 59 | struct InfoByHwMode { |
| 60 | typedef std::map<unsigned,InfoT> MapType; |
| 61 | typedef typename MapType::value_type PairType; |
| 62 | typedef typename MapType::iterator iterator; |
| 63 | typedef typename MapType::const_iterator const_iterator; |
| 64 | |
| 65 | InfoByHwMode() = default; |
| 66 | InfoByHwMode(const MapType &&M) : Map(M) {} |
| 67 | |
| 68 | iterator begin() { return Map.begin(); } |
| 69 | iterator end() { return Map.end(); } |
| 70 | const_iterator begin() const { return Map.begin(); } |
| 71 | const_iterator end() const { return Map.end(); } |
| 72 | bool empty() const { return Map.empty(); } |
| 73 | |
| 74 | bool hasMode(unsigned M) const { return Map.find(M) != Map.end(); } |
| 75 | bool hasDefault() const { return hasMode(DefaultMode); } |
| 76 | |
| 77 | InfoT &get(unsigned Mode) { |
| 78 | if (!hasMode(Mode)) { |
| 79 | assert(hasMode(DefaultMode)); |
| 80 | Map[Mode] = Map[DefaultMode]; |
| 81 | } |
| 82 | return Map[Mode]; |
| 83 | } |
| 84 | const InfoT &get(unsigned Mode) const { |
| 85 | auto F = Map.find(Mode); |
| 86 | if (Mode != DefaultMode && F == Map.end()) |
| 87 | F = Map.find(DefaultMode); |
| 88 | assert(F != Map.end()); |
| 89 | return F->second; |
| 90 | } |
| 91 | |
| 92 | bool isSimple() const { |
| 93 | return Map.size() == 1 && Map.begin()->first == DefaultMode; |
| 94 | } |
| 95 | InfoT getSimple() const { |
| 96 | assert(isSimple()); |
| 97 | return Map.begin()->second; |
| 98 | } |
| 99 | void makeSimple(unsigned Mode) { |
| 100 | assert(hasMode(Mode) || hasDefault()); |
| 101 | InfoT I = get(Mode); |
| 102 | Map.clear(); |
| 103 | Map.insert(std::make_pair(DefaultMode, I)); |
| 104 | } |
| 105 | |
| 106 | MapType Map; |
| 107 | }; |
| 108 | |
| 109 | struct ValueTypeByHwMode : public InfoByHwMode<MVT> { |
| 110 | ValueTypeByHwMode(Record *R, const CodeGenHwModes &CGH); |
| 111 | ValueTypeByHwMode(MVT T) { Map.insert({DefaultMode,T}); } |
| 112 | ValueTypeByHwMode() = default; |
| 113 | |
| 114 | bool operator== (const ValueTypeByHwMode &T) const; |
| 115 | bool operator< (const ValueTypeByHwMode &T) const; |
| 116 | |
| 117 | bool isValid() const { |
| 118 | return !Map.empty(); |
| 119 | } |
| 120 | MVT getType(unsigned Mode) const { return get(Mode); } |
| 121 | MVT &getOrCreateTypeForMode(unsigned Mode, MVT Type); |
| 122 | |
| 123 | static std::string getMVTName(MVT T); |
| 124 | std::string getAsString() const; |
| 125 | void dump() const; |
| 126 | }; |
| 127 | |
| 128 | ValueTypeByHwMode getValueTypeByHwMode(Record *Rec, |
| 129 | const CodeGenHwModes &CGH); |
| 130 | |
| 131 | struct RegSizeInfo { |
| 132 | unsigned RegSize; |
| 133 | unsigned SpillSize; |
| 134 | unsigned SpillAlignment; |
| 135 | |
| 136 | RegSizeInfo(Record *R, const CodeGenHwModes &CGH); |
| 137 | RegSizeInfo() = default; |
| 138 | bool operator< (const RegSizeInfo &I) const; |
| 139 | bool operator== (const RegSizeInfo &I) const { |
| 140 | return std::tie(RegSize, SpillSize, SpillAlignment) == |
| 141 | std::tie(I.RegSize, I.SpillSize, I.SpillAlignment); |
| 142 | } |
| 143 | bool operator!= (const RegSizeInfo &I) const { |
| 144 | return !(*this == I); |
| 145 | } |
| 146 | |
| 147 | bool isSubClassOf(const RegSizeInfo &I) const; |
| 148 | std::string getAsString() const; |
| 149 | }; |
| 150 | |
| 151 | struct RegSizeInfoByHwMode : public InfoByHwMode<RegSizeInfo> { |
| 152 | RegSizeInfoByHwMode(Record *R, const CodeGenHwModes &CGH); |
| 153 | RegSizeInfoByHwMode() = default; |
| 154 | bool operator< (const RegSizeInfoByHwMode &VI) const; |
| 155 | bool operator== (const RegSizeInfoByHwMode &VI) const; |
| 156 | bool operator!= (const RegSizeInfoByHwMode &VI) const { |
| 157 | return !(*this == VI); |
| 158 | } |
| 159 | |
| 160 | bool isSubClassOf(const RegSizeInfoByHwMode &I) const; |
| 161 | bool hasStricterSpillThan(const RegSizeInfoByHwMode &I) const; |
| 162 | |
| 163 | std::string getAsString() const; |
| 164 | }; |
| 165 | } // namespace llvm |
| 166 | |
| 167 | #endif // LLVM_UTILS_TABLEGEN_INFOBYHWMODE_H |