Index: test/TableGen/RegisterBankEmitter.td =================================================================== --- /dev/null +++ test/TableGen/RegisterBankEmitter.td @@ -0,0 +1,15 @@ +// RUN: llvm-tblgen -gen-register-bank -I %p/../../include %s | FileCheck %s + +include "llvm/Target/Target.td" + +def MyTarget : Target; +def R0 : Register<"r0">; +let Size = 32 in { + def ClassA : RegisterClass<"MyTarget", [i32], 32, (add R0)>; + def ClassB : RegisterClass<"MyTarget", [i1], 32, (add ClassA)>; +} + +// CHECK: GPRRegBankCoverageData +// CHECK: MyTarget::ClassARegClassID +// CHECK: MyTarget::ClassBRegClassID +def GPRRegBank : RegisterBank<"GPR", [ClassA]>; Index: utils/TableGen/RegisterBankEmitter.cpp =================================================================== --- utils/TableGen/RegisterBankEmitter.cpp +++ utils/TableGen/RegisterBankEmitter.cpp @@ -168,7 +168,14 @@ static void visitRegisterBankClasses( CodeGenRegBank &RegisterClassHierarchy, const CodeGenRegisterClass *RC, const Twine Kind, - std::function VisitFn) { + std::function VisitFn, + SmallPtrSetImpl &VisitedRCs) { + + // Make sure we only visit each class once to avoid infinite loops. + if (VisitedRCs.count(RC)) + return; + VisitedRCs.insert(RC); + // Visit each explicitly named class. VisitFn(RC, Kind.str()); @@ -180,7 +187,7 @@ if (RC != &PossibleSubclass && RC->hasSubClass(&PossibleSubclass)) visitRegisterBankClasses(RegisterClassHierarchy, &PossibleSubclass, TmpKind + " " + RC->getName() + " subclass", - VisitFn); + VisitFn, VisitedRCs); // Visit each class that contains only subregisters of RC with a common // subregister-index. @@ -273,6 +280,7 @@ std::vector Banks; for (const auto &V : Records.getAllDerivedDefinitions("RegisterBank")) { + SmallPtrSet VisitedRCs; RegisterBank Bank(*V); for (const CodeGenRegisterClass *RC : @@ -282,7 +290,7 @@ [&Bank](const CodeGenRegisterClass *RC, StringRef Kind) { DEBUG(dbgs() << "Added " << RC->getName() << "(" << Kind << ")\n"); Bank.addRegisterClass(RC); - }); + }, VisitedRCs); } Banks.push_back(Bank);