| 1 | //===- SubtargetFeatureInfo.cpp - Helpers for subtarget features ----------===// |
| 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 | |
| 9 | #include "SubtargetFeatureInfo.h" |
| 10 | #include "Types.h" |
| 11 | #include "llvm/Config/llvm-config.h" |
| 12 | #include "llvm/TableGen/Error.h" |
| 13 | #include "llvm/TableGen/Record.h" |
| 14 | |
| 15 | using namespace llvm; |
| 16 | |
| 17 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 18 | LLVM_DUMP_METHOD void SubtargetFeatureInfo::dump() const { |
| 19 | errs() << getEnumName() << " " << Index << "\n" << *TheDef; |
| 20 | } |
| 21 | #endif |
| 22 | |
| 23 | SubtargetFeaturesInfoVec |
| 24 | SubtargetFeatureInfo::getAll(const RecordKeeper &Records) { |
| 25 | SubtargetFeaturesInfoVec SubtargetFeatures; |
| 26 | for (const Record *Pred : Records.getAllDerivedDefinitions(ClassName: "Predicate" )) { |
| 27 | // Ignore predicates that are not intended for the assembler. |
| 28 | // |
| 29 | // The "AssemblerMatcherPredicate" string should be promoted to an argument |
| 30 | // if we re-use the machinery for non-assembler purposes in future. |
| 31 | if (!Pred->getValueAsBit(FieldName: "AssemblerMatcherPredicate" )) |
| 32 | continue; |
| 33 | |
| 34 | if (Pred->getName().empty()) |
| 35 | PrintFatalError(ErrorLoc: Pred->getLoc(), Msg: "Predicate has no name!" ); |
| 36 | |
| 37 | // Ignore always true predicates. |
| 38 | if (Pred->getValueAsString(FieldName: "CondString" ).empty()) |
| 39 | continue; |
| 40 | |
| 41 | SubtargetFeatures.emplace_back( |
| 42 | args&: Pred, args: SubtargetFeatureInfo(Pred, SubtargetFeatures.size())); |
| 43 | } |
| 44 | return SubtargetFeatures; |
| 45 | } |
| 46 | |
| 47 | void SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration( |
| 48 | const SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS, |
| 49 | const std::map<std::string, unsigned> *HwModes) { |
| 50 | OS << "// Bits for subtarget features that participate in " |
| 51 | << "instruction matching.\n" ; |
| 52 | unsigned Size = SubtargetFeatures.size(); |
| 53 | if (HwModes) |
| 54 | Size += HwModes->size(); |
| 55 | |
| 56 | OS << "enum SubtargetFeatureBits : " << getMinimalTypeForRange(Range: Size) |
| 57 | << " {\n" ; |
| 58 | for (const auto &SF : SubtargetFeatures) { |
| 59 | const SubtargetFeatureInfo &SFI = SF.second; |
| 60 | OS << " " << SFI.getEnumBitName() << " = " << SFI.Index << ",\n" ; |
| 61 | } |
| 62 | |
| 63 | if (HwModes) { |
| 64 | unsigned Offset = SubtargetFeatures.size(); |
| 65 | for (const auto &M : *HwModes) { |
| 66 | OS << " Feature_HwMode" << M.second << "Bit = " << (M.second + Offset) |
| 67 | << ",\n" ; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | OS << "};\n\n" ; |
| 72 | } |
| 73 | |
| 74 | void SubtargetFeatureInfo::emitNameTable( |
| 75 | SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) { |
| 76 | // Need to sort the name table so that lookup by the log of the enum value |
| 77 | // gives the proper name. More specifically, for a feature of value 1<<n, |
| 78 | // SubtargetFeatureNames[n] should be the name of the feature. |
| 79 | uint64_t IndexUB = 0; |
| 80 | for (const auto &SF : SubtargetFeatures) |
| 81 | if (IndexUB <= SF.second.Index) |
| 82 | IndexUB = SF.second.Index + 1; |
| 83 | |
| 84 | std::vector<std::string> Names; |
| 85 | if (IndexUB > 0) |
| 86 | Names.resize(new_size: IndexUB); |
| 87 | for (const auto &SF : SubtargetFeatures) |
| 88 | Names[SF.second.Index] = SF.second.getEnumName(); |
| 89 | |
| 90 | OS << "static const char *SubtargetFeatureNames[] = {\n" ; |
| 91 | for (uint64_t I = 0; I < IndexUB; ++I) |
| 92 | OS << " \"" << Names[I] << "\",\n" ; |
| 93 | |
| 94 | // A small number of targets have no predicates. Null terminate the array to |
| 95 | // avoid a zero-length array. |
| 96 | OS << " nullptr\n" |
| 97 | << "};\n\n" ; |
| 98 | } |
| 99 | |
| 100 | void SubtargetFeatureInfo::emitComputeAvailableFeatures( |
| 101 | StringRef TargetName, StringRef ClassName, StringRef FuncName, |
| 102 | const SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS, |
| 103 | StringRef , const std::map<std::string, unsigned> *HwModes) { |
| 104 | OS << "PredicateBitset " << ClassName << "::\n" |
| 105 | << FuncName << "(const " << TargetName << "Subtarget *Subtarget" ; |
| 106 | if (!ExtraParams.empty()) |
| 107 | OS << ", " << ExtraParams; |
| 108 | OS << ") const {\n" ; |
| 109 | OS << " PredicateBitset Features{};\n" ; |
| 110 | for (const auto &SF : SubtargetFeatures) { |
| 111 | const SubtargetFeatureInfo &SFI = SF.second; |
| 112 | StringRef CondStr = SFI.TheDef->getValueAsString(FieldName: "CondString" ); |
| 113 | assert(!CondStr.empty() && "true predicate should have been filtered" ); |
| 114 | |
| 115 | OS << " if (" << CondStr << ")\n" ; |
| 116 | OS << " Features.set(" << SFI.getEnumBitName() << ");\n" ; |
| 117 | } |
| 118 | |
| 119 | if (HwModes) { |
| 120 | for (const auto &M : *HwModes) { |
| 121 | OS << " if (" << M.first << ")\n" ; |
| 122 | OS << " Features.set(Feature_HwMode" << M.second << "Bit);\n" ; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | OS << " return Features;\n" ; |
| 127 | OS << "}\n\n" ; |
| 128 | } |
| 129 | |
| 130 | // If ParenIfBinOp is true, print a surrounding () if Val uses && or ||. |
| 131 | static bool emitFeaturesAux(StringRef TargetName, const Init &Val, |
| 132 | bool ParenIfBinOp, raw_ostream &OS) { |
| 133 | if (auto *D = dyn_cast<DefInit>(Val: &Val)) { |
| 134 | if (!D->getDef()->isSubClassOf(Name: "SubtargetFeature" )) |
| 135 | return true; |
| 136 | OS << "FB[" << TargetName << "::" << D->getAsString() << "]" ; |
| 137 | return false; |
| 138 | } |
| 139 | if (auto *D = dyn_cast<DagInit>(Val: &Val)) { |
| 140 | auto *Op = dyn_cast<DefInit>(Val: D->getOperator()); |
| 141 | if (!Op) |
| 142 | return true; |
| 143 | StringRef OpName = Op->getDef()->getName(); |
| 144 | if (OpName == "not" && D->getNumArgs() == 1) { |
| 145 | OS << '!'; |
| 146 | return emitFeaturesAux(TargetName, Val: *D->getArg(Num: 0), ParenIfBinOp: true, OS); |
| 147 | } |
| 148 | if ((OpName == "any_of" || OpName == "all_of" ) && D->getNumArgs() > 0) { |
| 149 | bool Paren = D->getNumArgs() > 1 && std::exchange(obj&: ParenIfBinOp, new_val: true); |
| 150 | if (Paren) |
| 151 | OS << '('; |
| 152 | ListSeparator LS(OpName == "any_of" ? " || " : " && " ); |
| 153 | for (auto *Arg : D->getArgs()) { |
| 154 | OS << LS; |
| 155 | if (emitFeaturesAux(TargetName, Val: *Arg, ParenIfBinOp, OS)) |
| 156 | return true; |
| 157 | } |
| 158 | if (Paren) |
| 159 | OS << ')'; |
| 160 | return false; |
| 161 | } |
| 162 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | void SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures( |
| 167 | StringRef TargetName, StringRef ClassName, StringRef FuncName, |
| 168 | SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) { |
| 169 | OS << "FeatureBitset " ; |
| 170 | if (!ClassName.empty()) |
| 171 | OS << TargetName << ClassName << "::\n" ; |
| 172 | OS << FuncName << "(const FeatureBitset &FB) " ; |
| 173 | if (!ClassName.empty()) |
| 174 | OS << "const " ; |
| 175 | OS << "{\n" ; |
| 176 | OS << " FeatureBitset Features;\n" ; |
| 177 | for (const auto &SF : SubtargetFeatures) { |
| 178 | const SubtargetFeatureInfo &SFI = SF.second; |
| 179 | |
| 180 | OS << " if (" ; |
| 181 | emitFeaturesAux(TargetName, Val: *SFI.TheDef->getValueAsDag(FieldName: "AssemblerCondDag" ), |
| 182 | /*ParenIfBinOp=*/false, OS); |
| 183 | OS << ")\n" ; |
| 184 | OS << " Features.set(" << SFI.getEnumBitName() << ");\n" ; |
| 185 | } |
| 186 | OS << " return Features;\n" ; |
| 187 | OS << "}\n\n" ; |
| 188 | } |
| 189 | |