Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / utils / TableGen / Attributes.cpp
blob474042a3e9a331860b312e38c5637d6bed8d6855
1 //===- Attributes.cpp - Generate attributes -------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "llvm/TableGen/Record.h"
10 #include "llvm/TableGen/TableGenBackend.h"
11 #include <vector>
12 using namespace llvm;
14 #define DEBUG_TYPE "attr-enum"
16 namespace {
18 class Attributes {
19 public:
20 Attributes(RecordKeeper &R) : Records(R) {}
21 void run(raw_ostream &OS);
23 private:
24 void emitTargetIndependentNames(raw_ostream &OS);
25 void emitFnAttrCompatCheck(raw_ostream &OS, bool IsStringAttr);
26 void emitAttributeProperties(raw_ostream &OF);
28 RecordKeeper &Records;
31 } // End anonymous namespace.
33 void Attributes::emitTargetIndependentNames(raw_ostream &OS) {
34 OS << "#ifdef GET_ATTR_NAMES\n";
35 OS << "#undef GET_ATTR_NAMES\n";
37 OS << "#ifndef ATTRIBUTE_ALL\n";
38 OS << "#define ATTRIBUTE_ALL(FIRST, SECOND)\n";
39 OS << "#endif\n\n";
41 auto Emit = [&](ArrayRef<StringRef> KindNames, StringRef MacroName) {
42 OS << "#ifndef " << MacroName << "\n";
43 OS << "#define " << MacroName
44 << "(FIRST, SECOND) ATTRIBUTE_ALL(FIRST, SECOND)\n";
45 OS << "#endif\n\n";
46 for (StringRef KindName : KindNames) {
47 for (auto *A : Records.getAllDerivedDefinitions(KindName)) {
48 OS << MacroName << "(" << A->getName() << ","
49 << A->getValueAsString("AttrString") << ")\n";
52 OS << "#undef " << MacroName << "\n\n";
55 // Emit attribute enums in the same order llvm::Attribute::operator< expects.
56 Emit({"EnumAttr", "TypeAttr", "IntAttr"}, "ATTRIBUTE_ENUM");
57 Emit({"StrBoolAttr"}, "ATTRIBUTE_STRBOOL");
58 Emit({"ComplexStrAttr"}, "ATTRIBUTE_COMPLEXSTR");
60 OS << "#undef ATTRIBUTE_ALL\n";
61 OS << "#endif\n\n";
63 OS << "#ifdef GET_ATTR_ENUM\n";
64 OS << "#undef GET_ATTR_ENUM\n";
65 unsigned Value = 1; // Leave zero for AttrKind::None.
66 for (StringRef KindName : {"EnumAttr", "TypeAttr", "IntAttr"}) {
67 OS << "First" << KindName << " = " << Value << ",\n";
68 for (auto *A : Records.getAllDerivedDefinitions(KindName)) {
69 OS << A->getName() << " = " << Value << ",\n";
70 Value++;
72 OS << "Last" << KindName << " = " << (Value - 1) << ",\n";
74 OS << "#endif\n\n";
77 void Attributes::emitFnAttrCompatCheck(raw_ostream &OS, bool IsStringAttr) {
78 OS << "#ifdef GET_ATTR_COMPAT_FUNC\n";
79 OS << "#undef GET_ATTR_COMPAT_FUNC\n";
81 OS << "static inline bool hasCompatibleFnAttrs(const Function &Caller,\n"
82 << " const Function &Callee) {\n";
83 OS << " bool Ret = true;\n\n";
85 std::vector<Record *> CompatRules =
86 Records.getAllDerivedDefinitions("CompatRule");
88 for (auto *Rule : CompatRules) {
89 StringRef FuncName = Rule->getValueAsString("CompatFunc");
90 OS << " Ret &= " << FuncName << "(Caller, Callee);\n";
93 OS << "\n";
94 OS << " return Ret;\n";
95 OS << "}\n\n";
97 std::vector<Record *> MergeRules =
98 Records.getAllDerivedDefinitions("MergeRule");
99 OS << "static inline void mergeFnAttrs(Function &Caller,\n"
100 << " const Function &Callee) {\n";
102 for (auto *Rule : MergeRules) {
103 StringRef FuncName = Rule->getValueAsString("MergeFunc");
104 OS << " " << FuncName << "(Caller, Callee);\n";
107 OS << "}\n\n";
109 OS << "#endif\n";
112 void Attributes::emitAttributeProperties(raw_ostream &OS) {
113 OS << "#ifdef GET_ATTR_PROP_TABLE\n";
114 OS << "#undef GET_ATTR_PROP_TABLE\n";
115 OS << "static const uint8_t AttrPropTable[] = {\n";
116 for (StringRef KindName : {"EnumAttr", "TypeAttr", "IntAttr"}) {
117 for (auto *A : Records.getAllDerivedDefinitions(KindName)) {
118 OS << "0";
119 for (Init *P : *A->getValueAsListInit("Properties"))
120 OS << " | AttributeProperty::" << cast<DefInit>(P)->getDef()->getName();
121 OS << ",\n";
124 OS << "};\n";
125 OS << "#endif\n";
128 void Attributes::run(raw_ostream &OS) {
129 emitTargetIndependentNames(OS);
130 emitFnAttrCompatCheck(OS, false);
131 emitAttributeProperties(OS);
134 static TableGen::Emitter::OptClass<Attributes> X("gen-attrs",
135 "Generate attributes");