Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 1 | //===--- InfoByHwMode.cpp -------------------------------------------------===// |
| 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 | #include "CodeGenTarget.h" |
| 16 | #include "InfoByHwMode.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/Twine.h" |
| 19 | #include "llvm/Support/Debug.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | |
| 22 | #include <set> |
| 23 | #include <sstream> |
| 24 | #include <string> |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | std::string llvm::getModeName(unsigned Mode) { |
| 29 | if (Mode == DefaultMode) |
| 30 | return "*"; |
| 31 | return (Twine('m') + Twine(Mode)).str(); |
| 32 | } |
| 33 | |
| 34 | ValueTypeByHwMode::ValueTypeByHwMode(Record *R, const CodeGenHwModes &CGH) { |
| 35 | const HwModeSelect &MS = CGH.getHwModeSelect(R); |
| 36 | for (const HwModeSelect::PairType &P : MS.Items) { |
| 37 | auto I = Map.insert({P.first, MVT(llvm::getValueType(P.second))}); |
| 38 | assert(I.second && "Duplicate entry?"); |
| 39 | (void)I; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | bool ValueTypeByHwMode::operator== (const ValueTypeByHwMode &T) const { |
| 44 | assert(isValid() && T.isValid() && "Invalid type in assignment"); |
| 45 | bool Simple = isSimple(); |
| 46 | if (Simple != T.isSimple()) |
| 47 | return false; |
| 48 | if (Simple) |
| 49 | return getSimple() == T.getSimple(); |
| 50 | |
| 51 | return Map == T.Map; |
| 52 | } |
| 53 | |
| 54 | bool ValueTypeByHwMode::operator< (const ValueTypeByHwMode &T) const { |
| 55 | assert(isValid() && T.isValid() && "Invalid type in comparison"); |
| 56 | // Default order for maps. |
| 57 | return Map < T.Map; |
| 58 | } |
| 59 | |
| 60 | MVT &ValueTypeByHwMode::getOrCreateTypeForMode(unsigned Mode, MVT Type) { |
| 61 | auto F = Map.find(Mode); |
| 62 | if (F != Map.end()) |
| 63 | return F->second; |
| 64 | // If Mode is not in the map, look up the default mode. If it exists, |
| 65 | // make a copy of it for Mode and return it. |
| 66 | auto D = Map.find(DefaultMode); |
| 67 | if (D != Map.end()) |
| 68 | return Map.insert(std::make_pair(Mode, D->second)).first->second; |
| 69 | // If default mode is not present either, use provided Type. |
| 70 | return Map.insert(std::make_pair(Mode, Type)).first->second; |
| 71 | } |
| 72 | |
Simon Pilgrim | 6f05a74 | 2017-09-22 13:32:26 +0000 | [diff] [blame^] | 73 | StringRef ValueTypeByHwMode::getMVTName(MVT T) { |
| 74 | StringRef N = llvm::getEnumName(T.SimpleTy); |
| 75 | N.consume_front("MVT::"); |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 76 | return N; |
| 77 | } |
| 78 | |
| 79 | std::string ValueTypeByHwMode::getAsString() const { |
| 80 | if (isSimple()) |
| 81 | return getMVTName(getSimple()); |
| 82 | |
| 83 | std::vector<const PairType*> Pairs; |
| 84 | for (const auto &P : Map) |
| 85 | Pairs.push_back(&P); |
| 86 | std::sort(Pairs.begin(), Pairs.end(), deref<std::less<PairType>>()); |
| 87 | |
| 88 | std::stringstream str; |
| 89 | str << '{'; |
| 90 | for (unsigned i = 0, e = Pairs.size(); i != e; ++i) { |
| 91 | const PairType *P = Pairs[i]; |
| 92 | str << '(' << getModeName(P->first) |
Simon Pilgrim | 6f05a74 | 2017-09-22 13:32:26 +0000 | [diff] [blame^] | 93 | << ':' << getMVTName(P->second).str() << ')'; |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 94 | if (i != e-1) |
| 95 | str << ','; |
| 96 | } |
| 97 | str << '}'; |
| 98 | return str.str(); |
| 99 | } |
| 100 | |
| 101 | LLVM_DUMP_METHOD |
| 102 | void ValueTypeByHwMode::dump() const { |
| 103 | dbgs() << "size=" << Map.size() << '\n'; |
| 104 | for (const auto &P : Map) |
| 105 | dbgs() << " " << P.first << " -> " |
| 106 | << llvm::getEnumName(P.second.SimpleTy) << '\n'; |
| 107 | } |
| 108 | |
| 109 | ValueTypeByHwMode llvm::getValueTypeByHwMode(Record *Rec, |
| 110 | const CodeGenHwModes &CGH) { |
| 111 | #ifndef NDEBUG |
| 112 | if (!Rec->isSubClassOf("ValueType")) |
| 113 | Rec->dump(); |
| 114 | #endif |
| 115 | assert(Rec->isSubClassOf("ValueType") && |
| 116 | "Record must be derived from ValueType"); |
| 117 | if (Rec->isSubClassOf("HwModeSelect")) |
| 118 | return ValueTypeByHwMode(Rec, CGH); |
| 119 | return ValueTypeByHwMode(llvm::getValueType(Rec)); |
| 120 | } |
| 121 | |
| 122 | RegSizeInfo::RegSizeInfo(Record *R, const CodeGenHwModes &CGH) { |
| 123 | RegSize = R->getValueAsInt("RegSize"); |
| 124 | SpillSize = R->getValueAsInt("SpillSize"); |
| 125 | SpillAlignment = R->getValueAsInt("SpillAlignment"); |
| 126 | } |
| 127 | |
| 128 | bool RegSizeInfo::operator< (const RegSizeInfo &I) const { |
| 129 | return std::tie(RegSize, SpillSize, SpillAlignment) < |
| 130 | std::tie(I.RegSize, I.SpillSize, I.SpillAlignment); |
| 131 | } |
| 132 | |
| 133 | bool RegSizeInfo::isSubClassOf(const RegSizeInfo &I) const { |
| 134 | return RegSize <= I.RegSize && |
| 135 | SpillAlignment && I.SpillAlignment % SpillAlignment == 0 && |
| 136 | SpillSize <= I.SpillSize; |
| 137 | } |
| 138 | |
| 139 | std::string RegSizeInfo::getAsString() const { |
| 140 | std::stringstream str; |
| 141 | str << "[R=" << RegSize << ",S=" << SpillSize |
| 142 | << ",A=" << SpillAlignment << ']'; |
| 143 | return str.str(); |
| 144 | } |
| 145 | |
| 146 | RegSizeInfoByHwMode::RegSizeInfoByHwMode(Record *R, |
| 147 | const CodeGenHwModes &CGH) { |
| 148 | const HwModeSelect &MS = CGH.getHwModeSelect(R); |
| 149 | for (const HwModeSelect::PairType &P : MS.Items) { |
| 150 | auto I = Map.insert({P.first, RegSizeInfo(P.second, CGH)}); |
| 151 | assert(I.second && "Duplicate entry?"); |
| 152 | (void)I; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | bool RegSizeInfoByHwMode::operator< (const RegSizeInfoByHwMode &I) const { |
| 157 | unsigned M0 = Map.begin()->first; |
| 158 | return get(M0) < I.get(M0); |
| 159 | } |
| 160 | |
| 161 | bool RegSizeInfoByHwMode::operator== (const RegSizeInfoByHwMode &I) const { |
| 162 | unsigned M0 = Map.begin()->first; |
| 163 | return get(M0) == I.get(M0); |
| 164 | } |
| 165 | |
| 166 | bool RegSizeInfoByHwMode::isSubClassOf(const RegSizeInfoByHwMode &I) const { |
| 167 | unsigned M0 = Map.begin()->first; |
| 168 | return get(M0).isSubClassOf(I.get(M0)); |
| 169 | } |
| 170 | |
| 171 | bool RegSizeInfoByHwMode::hasStricterSpillThan(const RegSizeInfoByHwMode &I) |
| 172 | const { |
| 173 | unsigned M0 = Map.begin()->first; |
| 174 | const RegSizeInfo &A0 = get(M0); |
| 175 | const RegSizeInfo &B0 = I.get(M0); |
| 176 | return std::tie(A0.SpillSize, A0.SpillAlignment) > |
| 177 | std::tie(B0.SpillSize, B0.SpillAlignment); |
| 178 | } |
| 179 | |
| 180 | std::string RegSizeInfoByHwMode::getAsString() const { |
| 181 | typedef typename decltype(Map)::value_type PairType; |
| 182 | std::vector<const PairType*> Pairs; |
| 183 | for (const auto &P : Map) |
| 184 | Pairs.push_back(&P); |
| 185 | std::sort(Pairs.begin(), Pairs.end(), deref<std::less<PairType>>()); |
| 186 | |
| 187 | std::stringstream str; |
| 188 | str << '{'; |
| 189 | for (unsigned i = 0, e = Pairs.size(); i != e; ++i) { |
| 190 | const PairType *P = Pairs[i]; |
| 191 | str << '(' << getModeName(P->first) |
| 192 | << ':' << P->second.getAsString() << ')'; |
| 193 | if (i != e-1) |
| 194 | str << ','; |
| 195 | } |
| 196 | str << '}'; |
| 197 | return str.str(); |
| 198 | } |