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