Skip to content

Commit 350fafa

Browse files
author
Paul C. Anagnostopoulos
committed
[TableGen] Add overload of RecordKeeper::getAllDerivedDefinitions()
and use in PseudoLowering backend. Now the two getAllDerivedDefinitions() use StringRef and Arrayref. Use all_of() in getAllDerivedDefinitions().
1 parent 09ee1fe commit 350fafa

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

llvm/docs/TableGen/BackGuide.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,9 @@ The ``RecordKeeper`` class provides four functions for getting the
569569
``Record`` references for the concrete records that derive from the
570570
given class.
571571

572-
* ``getAllDerivedDefinitionsTwo(``\ *classname1*\ ``,`` *classname2*\ ``)`` returns
572+
* ``getAllDerivedDefinitions(``\ *classnames*\ ``)`` returns
573573
a vector of ``Record`` references for the concrete records that derive from
574-
*both* of the given classes. [function to come]
574+
*all* of the given classes.
575575

576576
This statement obtains all the records that derive from the ``Attribute``
577577
class and iterates over them.

llvm/include/llvm/TableGen/Record.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,9 +1784,17 @@ class RecordKeeper {
17841784
//===--------------------------------------------------------------------===//
17851785
// High-level helper methods, useful for tablegen backends.
17861786

1787-
/// Get all the concrete records that inherit from the specified
1787+
/// Get all the concrete records that inherit from all the specified
1788+
/// classes. The classes must be defined.
1789+
std::vector<Record *> getAllDerivedDefinitions(
1790+
const ArrayRef<StringRef> ClassNames) const;
1791+
1792+
/// Get all the concrete records that inherit from the one specified
17881793
/// class. The class must be defined.
1789-
std::vector<Record *> getAllDerivedDefinitions(StringRef ClassName) const;
1794+
std::vector<Record *> getAllDerivedDefinitions(StringRef ClassName) const {
1795+
1796+
return getAllDerivedDefinitions(makeArrayRef(ClassName));
1797+
}
17901798

17911799
void dump() const;
17921800
};

llvm/lib/TableGen/Record.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2470,16 +2470,25 @@ Init *RecordKeeper::getNewAnonymousName() {
24702470
return StringInit::get("anonymous_" + utostr(AnonCounter++));
24712471
}
24722472

2473-
std::vector<Record *>
2474-
RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const {
2475-
Record *Class = getClass(ClassName);
2476-
if (!Class)
2477-
PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n");
2473+
std::vector<Record *> RecordKeeper::getAllDerivedDefinitions(
2474+
const ArrayRef<StringRef> ClassNames) const {
2475+
SmallVector<Record *, 2> ClassRecs;
2476+
std::vector<Record *> Defs;
24782477

2479-
std::vector<Record*> Defs;
2480-
for (const auto &D : getDefs())
2481-
if (D.second->isSubClassOf(Class))
2482-
Defs.push_back(D.second.get());
2478+
assert(ClassNames.size() > 0 && "At least one class must be passed.");
2479+
for (const auto &ClassName : ClassNames) {
2480+
Record *Class = getClass(ClassName);
2481+
if (!Class)
2482+
PrintFatalError("The class '" + ClassName + "' is not defined\n");
2483+
ClassRecs.push_back(Class);
2484+
}
2485+
2486+
for (const auto &OneDef : getDefs()) {
2487+
if (all_of(ClassRecs, [&OneDef](const Record *Class) {
2488+
return OneDef.second->isSubClassOf(Class);
2489+
}))
2490+
Defs.push_back(OneDef.second.get());
2491+
}
24832492

24842493
return Defs;
24852494
}

llvm/utils/TableGen/PseudoLoweringEmitter.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,9 @@ void PseudoLoweringEmitter::emitLoweringEmitter(raw_ostream &o) {
293293
}
294294

295295
void PseudoLoweringEmitter::run(raw_ostream &o) {
296-
Record *ExpansionClass = Records.getClass("PseudoInstExpansion");
297-
Record *InstructionClass = Records.getClass("Instruction");
298-
assert(ExpansionClass && "PseudoInstExpansion class definition missing!");
299-
assert(InstructionClass && "Instruction class definition missing!");
300-
301-
std::vector<Record*> Insts;
302-
for (const auto &D : Records.getDefs()) {
303-
if (D.second->isSubClassOf(ExpansionClass) &&
304-
D.second->isSubClassOf(InstructionClass))
305-
Insts.push_back(D.second.get());
306-
}
296+
StringRef Classes[] = {"PseudoInstExpansion", "Instruction"};
297+
std::vector<Record *> Insts =
298+
Records.getAllDerivedDefinitions(makeArrayRef(Classes));
307299

308300
// Process the pseudo expansion definitions, validating them as we do so.
309301
for (unsigned i = 0, e = Insts.size(); i != e; ++i)

0 commit comments

Comments
 (0)