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 11 #include "Types.h" 12 #include "llvm/Config/llvm-config.h" 13 #include "llvm/TableGen/Record.h" 14 15 #include <map> 16 17 using namespace llvm; 18 19 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 20 LLVM_DUMP_METHOD void SubtargetFeatureInfo::dump() const { 21 errs() << getEnumName() << " " << Index << "\n" << *TheDef; 22 } 23 #endif 24 25 std::vector<std::pair<Record *, SubtargetFeatureInfo>> 26 SubtargetFeatureInfo::getAll(const RecordKeeper &Records) { 27 std::vector<std::pair<Record *, SubtargetFeatureInfo>> SubtargetFeatures; 28 std::vector<Record *> AllPredicates = 29 Records.getAllDerivedDefinitions("Predicate"); 30 for (Record *Pred : AllPredicates) { 31 // Ignore predicates that are not intended for the assembler. 32 // 33 // The "AssemblerMatcherPredicate" string should be promoted to an argument 34 // if we re-use the machinery for non-assembler purposes in future. 35 if (!Pred->getValueAsBit("AssemblerMatcherPredicate")) 36 continue; 37 38 if (Pred->getName().empty()) 39 PrintFatalError(Pred->getLoc(), "Predicate has no name!"); 40 41 SubtargetFeatures.emplace_back( 42 Pred, SubtargetFeatureInfo(Pred, SubtargetFeatures.size())); 43 } 44 return SubtargetFeatures; 45 } 46 47 void SubtargetFeatureInfo::emitSubtargetFeatureFlagEnumeration( 48 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) { 49 OS << "// Flags for subtarget features that participate in " 50 << "instruction matching.\n"; 51 OS << "enum SubtargetFeatureFlag : " 52 << getMinimalTypeForEnumBitfield(SubtargetFeatures.size()) << " {\n"; 53 for (const auto &SF : SubtargetFeatures) { 54 const SubtargetFeatureInfo &SFI = SF.second; 55 OS << " " << SFI.getEnumName() << " = (1ULL << " << SFI.Index << "),\n"; 56 } 57 OS << " Feature_None = 0\n"; 58 OS << "};\n\n"; 59 } 60 61 void SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration( 62 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) { 63 OS << "// Bits for subtarget features that participate in " 64 << "instruction matching.\n"; 65 OS << "enum SubtargetFeatureBits : " 66 << getMinimalTypeForRange(SubtargetFeatures.size()) << " {\n"; 67 for (const auto &SF : SubtargetFeatures) { 68 const SubtargetFeatureInfo &SFI = SF.second; 69 OS << " " << SFI.getEnumBitName() << " = " << SFI.Index << ",\n"; 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(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 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS, 103 StringRef ExtraParams) { 104 OS << "PredicateBitset " << TargetName << 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 113 OS << " if (" << SFI.TheDef->getValueAsString("CondString") << ")\n"; 114 OS << " Features[" << SFI.getEnumBitName() << "] = 1;\n"; 115 } 116 OS << " return Features;\n"; 117 OS << "}\n\n"; 118 } 119 120 void SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures( 121 StringRef TargetName, StringRef ClassName, StringRef FuncName, 122 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) { 123 OS << "uint64_t " << TargetName << ClassName << "::\n" 124 << FuncName << "(const FeatureBitset& FB) const {\n"; 125 OS << " uint64_t Features = 0;\n"; 126 for (const auto &SF : SubtargetFeatures) { 127 const SubtargetFeatureInfo &SFI = SF.second; 128 129 OS << " if ("; 130 std::string CondStorage = 131 SFI.TheDef->getValueAsString("AssemblerCondString"); 132 StringRef Conds = CondStorage; 133 std::pair<StringRef, StringRef> Comma = Conds.split(','); 134 bool First = true; 135 do { 136 if (!First) 137 OS << " && "; 138 139 bool Neg = false; 140 StringRef Cond = Comma.first; 141 if (Cond[0] == '!') { 142 Neg = true; 143 Cond = Cond.substr(1); 144 } 145 146 OS << "("; 147 if (Neg) 148 OS << "!"; 149 OS << "FB[" << TargetName << "::" << Cond << "])"; 150 151 if (Comma.second.empty()) 152 break; 153 154 First = false; 155 Comma = Comma.second.split(','); 156 } while (true); 157 158 OS << ")\n"; 159 OS << " Features |= " << SFI.getEnumName() << ";\n"; 160 } 161 OS << " return Features;\n"; 162 OS << "}\n\n"; 163 } 164