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 "llvm/TableGen/Record.h"
13 
14 #include <map>
15 
16 using namespace llvm;
17 
18 void SubtargetFeatureInfo::dump() const {
19   errs() << getEnumName() << " " << Index << "\n";
20   TheDef->dump();
21 }
22 
23 std::vector<std::pair<Record *, SubtargetFeatureInfo>>
24 SubtargetFeatureInfo::getAll(const RecordKeeper &Records) {
25   std::vector<std::pair<Record *, SubtargetFeatureInfo>> SubtargetFeatures;
26   std::vector<Record *> AllPredicates =
27       Records.getAllDerivedDefinitions("Predicate");
28   for (Record *Pred : AllPredicates) {
29     // Ignore predicates that are not intended for the assembler.
30     //
31     // The "AssemblerMatcherPredicate" string should be promoted to an argument
32     // if we re-use the machinery for non-assembler purposes in future.
33     if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
34       continue;
35 
36     if (Pred->getName().empty())
37       PrintFatalError(Pred->getLoc(), "Predicate has no name!");
38 
39     SubtargetFeatures.emplace_back(
40         Pred, SubtargetFeatureInfo(Pred, SubtargetFeatures.size()));
41   }
42   return SubtargetFeatures;
43 }
44 
45 void SubtargetFeatureInfo::emitComputeAvailableFeatures(
46     StringRef TargetName, StringRef ClassName,
47     std::map<Record *, SubtargetFeatureInfo, LessRecordByID> &SubtargetFeatures,
48     raw_ostream &OS) {
49   OS << "uint64_t " << TargetName << ClassName << "::\n"
50      << "ComputeAvailableFeatures(const FeatureBitset& FB) const {\n";
51   OS << "  uint64_t Features = 0;\n";
52   for (const auto &SF : SubtargetFeatures) {
53     const SubtargetFeatureInfo &SFI = SF.second;
54 
55     OS << "  if (";
56     std::string CondStorage =
57         SFI.TheDef->getValueAsString("AssemblerCondString");
58     StringRef Conds = CondStorage;
59     std::pair<StringRef, StringRef> Comma = Conds.split(',');
60     bool First = true;
61     do {
62       if (!First)
63         OS << " && ";
64 
65       bool Neg = false;
66       StringRef Cond = Comma.first;
67       if (Cond[0] == '!') {
68         Neg = true;
69         Cond = Cond.substr(1);
70       }
71 
72       OS << "(";
73       if (Neg)
74         OS << "!";
75       OS << "FB[" << TargetName << "::" << Cond << "])";
76 
77       if (Comma.second.empty())
78         break;
79 
80       First = false;
81       Comma = Comma.second.split(',');
82     } while (true);
83 
84     OS << ")\n";
85     OS << "    Features |= " << SFI.getEnumName() << ";\n";
86   }
87   OS << "  return Features;\n";
88   OS << "}\n\n";
89 }
90