Index: llvm/test/TableGen/Common/reg-with-subregs-common.td =================================================================== --- llvm/test/TableGen/Common/reg-with-subregs-common.td +++ llvm/test/TableGen/Common/reg-with-subregs-common.td @@ -16,6 +16,9 @@ !listconcat(acc, !if(!lt(cur, N), [cur], []))); } +#ifdef USE_NAMESPACE + let Namespace = "TestNamespace" in { +#endif foreach Index = 0-31 in { def sub#Index : SubRegIndex<32, !shl(Index, 5)>; } @@ -35,6 +38,9 @@ foreach Index = 0-255 in { def R#Index : Register <"r"#Index>; } +#ifdef USE_NAMESPACE +} +#endif def GPR32 : RegisterClass<"TestTarget", [i32], 32, (add (sequence "R%u", 0, 255))>; @@ -124,5 +130,11 @@ (decimate (shl GPR32, 31), 1) ]>; +#ifdef USE_NAMESPACE + let Namespace = "TestNamespace" in { +#endif def GPR_64 : RegisterClass<"", [v2i32], 64, (add GPR64)>; def GPR_1024 : RegisterClass<"", [v32i32], 1024, (add GPR1024)>; +#ifdef USE_NAMESPACE +} +#endif Index: llvm/test/TableGen/pset-enum.td =================================================================== --- /dev/null +++ llvm/test/TableGen/pset-enum.td @@ -0,0 +1,11 @@ +// RUN: llvm-tblgen -gen-register-info -I %p/../../include -I %p/Common %s | FileCheck %s +// RUN: llvm-tblgen -gen-register-info -I %p/../../include -I %p/Common -DUSE_NAMESPACE %s | FileCheck --check-prefixes=CHECK,NAMESPACE %s + +include "reg-with-subregs-common.td" + +// CHECK-LABEL: // Register pressure sets enum. +// NAMESPACE-NEXT: namespace TestNamespace { +// CHECK-NEXT: enum RegisterPressureSets { +// CHECK-NEXT: GPR32 = 0, +// CHECK-NEXT: }; +// NAMESPACE-NEXT: } // end namespace TestNamespace Index: llvm/utils/TableGen/RegisterInfoEmitter.cpp =================================================================== --- llvm/utils/TableGen/RegisterInfoEmitter.cpp +++ llvm/utils/TableGen/RegisterInfoEmitter.cpp @@ -190,11 +190,23 @@ OS << Val; } +static std::string getLegalCName(std::string Name) { + static const char legal_elements[] = + "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + std::string::size_type pos = 0; + while ((pos = Name.find_first_not_of(legal_elements, pos)) != + std::string::npos) + Name.replace(pos, 1, "_"); + return Name; +} + void RegisterInfoEmitter:: EmitRegUnitPressure(raw_ostream &OS, const CodeGenRegBank &RegBank, const std::string &ClassName) { unsigned NumRCs = RegBank.getRegClasses().size(); unsigned NumSets = RegBank.getNumRegPressureSets(); + StringRef Namespace = RegBank.getRegisters().front().TheDef-> + getValueAsString("Namespace"); OS << "/// Get the weight in units of pressure for this register class.\n" << "const RegClassWeight &" << ClassName << "::\n" @@ -251,14 +263,27 @@ << "unsigned " << ClassName << "::getNumRegPressureSets() const {\n" << " return " << NumSets << ";\n}\n\n"; + OS << "// Register pressure sets enum.\n"; + if (!Namespace.empty()) + OS << "namespace " << Namespace << " {\n"; + OS << "enum RegisterPressureSets {\n"; + unsigned MaxRegUnitWeight = 0; + for (unsigned i = 0; i < NumSets; ++i ) { + const RegUnitSet &RegUnits = RegBank.getRegSetAt(i); + MaxRegUnitWeight = std::max(MaxRegUnitWeight, RegUnits.Weight); + OS << " " << getLegalCName(RegUnits.Name) << " = " << i << ",\n"; + } + OS << "};\n"; + if (!Namespace.empty()) + OS << "} // end namespace " << Namespace << '\n'; + OS << '\n'; + OS << "// Get the name of this register unit pressure set.\n" << "const char *" << ClassName << "::\n" << "getRegPressureSetName(unsigned Idx) const {\n" << " static const char *const PressureNameTable[] = {\n"; - unsigned MaxRegUnitWeight = 0; for (unsigned i = 0; i < NumSets; ++i ) { const RegUnitSet &RegUnits = RegBank.getRegSetAt(i); - MaxRegUnitWeight = std::max(MaxRegUnitWeight, RegUnits.Weight); OS << " \"" << RegUnits.Name << "\",\n"; } OS << " };\n"