1 //===- SubtargetFeatureInfo.cpp - Helpers for subtarget features ----------===// 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 10 #include "SubtargetFeatureInfo.h" 11 12 #include "Types.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 std::map<Record *, SubtargetFeatureInfo, LessRecordByID> &SubtargetFeatures, 49 raw_ostream &OS) { 50 OS << "// Flags for subtarget features that participate in " 51 << "instruction matching.\n"; 52 OS << "enum SubtargetFeatureFlag : " 53 << getMinimalTypeForEnumBitfield(SubtargetFeatures.size()) << " {\n"; 54 for (const auto &SF : SubtargetFeatures) { 55 const SubtargetFeatureInfo &SFI = SF.second; 56 OS << " " << SFI.getEnumName() << " = (1ULL << " << SFI.Index << "),\n"; 57 } 58 OS << " Feature_None = 0\n"; 59 OS << "};\n\n"; 60 } 61 62 void SubtargetFeatureInfo::emitNameTable( 63 std::map<Record *, SubtargetFeatureInfo, LessRecordByID> &SubtargetFeatures, 64 raw_ostream &OS) { 65 OS << "static const char *SubtargetFeatureNames[] = {\n"; 66 for (const auto &SF : SubtargetFeatures) { 67 const SubtargetFeatureInfo &SFI = SF.second; 68 OS << " \"" << SFI.getEnumName() << "\",\n"; 69 } 70 // A small number of targets have no predicates. Null terminate the array to 71 // avoid a zero-length array. 72 OS << " nullptr\n" 73 << "};\n\n"; 74 } 75 76 void SubtargetFeatureInfo::emitComputeAvailableFeatures( 77 StringRef TargetName, StringRef ClassName, StringRef FuncName, 78 std::map<Record *, SubtargetFeatureInfo, LessRecordByID> &SubtargetFeatures, 79 raw_ostream &OS) { 80 OS << "uint64_t " << TargetName << ClassName << "::\n" 81 << FuncName << "(const FeatureBitset& FB) const {\n"; 82 OS << " uint64_t Features = 0;\n"; 83 for (const auto &SF : SubtargetFeatures) { 84 const SubtargetFeatureInfo &SFI = SF.second; 85 86 OS << " if ("; 87 std::string CondStorage = 88 SFI.TheDef->getValueAsString("AssemblerCondString"); 89 StringRef Conds = CondStorage; 90 std::pair<StringRef, StringRef> Comma = Conds.split(','); 91 bool First = true; 92 do { 93 if (!First) 94 OS << " && "; 95 96 bool Neg = false; 97 StringRef Cond = Comma.first; 98 if (Cond[0] == '!') { 99 Neg = true; 100 Cond = Cond.substr(1); 101 } 102 103 OS << "("; 104 if (Neg) 105 OS << "!"; 106 OS << "FB[" << TargetName << "::" << Cond << "])"; 107 108 if (Comma.second.empty()) 109 break; 110 111 First = false; 112 Comma = Comma.second.split(','); 113 } while (true); 114 115 OS << ")\n"; 116 OS << " Features |= " << SFI.getEnumName() << ";\n"; 117 } 118 OS << " return Features;\n"; 119 OS << "}\n\n"; 120 } 121