Skip to content

Commit 8a36eb8

Browse files
authored
[LLVM][TableGen] Change AsmMatcherEmitter to use const RecordKeeper (#109174)
Change AsmMatcherEmitter to use const RecordKeeper. This is a part of effort to have better const correctness in TableGen backends: https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089
1 parent 2c90eb9 commit 8a36eb8

File tree

1 file changed

+35
-38
lines changed

1 file changed

+35
-38
lines changed

llvm/utils/TableGen/AsmMatcherEmitter.cpp

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ class AsmMatcherInfo;
143143
typedef std::set<const Record *, LessRecordByID> RegisterSet;
144144

145145
class AsmMatcherEmitter {
146-
RecordKeeper &Records;
146+
const RecordKeeper &Records;
147147

148148
public:
149-
AsmMatcherEmitter(RecordKeeper &R) : Records(R) {}
149+
AsmMatcherEmitter(const RecordKeeper &R) : Records(R) {}
150150

151151
void run(raw_ostream &o);
152152
};
@@ -739,13 +739,13 @@ struct OperandMatchEntry {
739739
class AsmMatcherInfo {
740740
public:
741741
/// Tracked Records
742-
RecordKeeper &Records;
742+
const RecordKeeper &Records;
743743

744744
/// The tablegen AsmParser record.
745-
Record *AsmParser;
745+
const Record *AsmParser;
746746

747747
/// Target - The target information.
748-
CodeGenTarget &Target;
748+
const CodeGenTarget &Target;
749749

750750
/// The classes which are needed for matching.
751751
std::forward_list<ClassInfo> Classes;
@@ -798,8 +798,8 @@ class AsmMatcherInfo {
798798
MatchableInfo::AsmOperand &Op);
799799

800800
public:
801-
AsmMatcherInfo(Record *AsmParser, CodeGenTarget &Target,
802-
RecordKeeper &Records);
801+
AsmMatcherInfo(const Record *AsmParser, const CodeGenTarget &Target,
802+
const RecordKeeper &Records);
803803

804804
/// Construct the various tables used during matching.
805805
void buildInfo();
@@ -816,7 +816,7 @@ class AsmMatcherInfo {
816816
return I == SubtargetFeatures.end() ? nullptr : &I->second;
817817
}
818818

819-
RecordKeeper &getRecords() const { return Records; }
819+
const RecordKeeper &getRecords() const { return Records; }
820820

821821
bool hasOptionalOperands() const {
822822
return any_of(Classes,
@@ -965,7 +965,7 @@ void MatchableInfo::initialize(
965965
Mnemonic = AsmOperands[0].Token;
966966

967967
// Compute the require features.
968-
for (Record *Predicate : TheDef->getValueAsListOfDefs("Predicates"))
968+
for (const Record *Predicate : TheDef->getValueAsListOfDefs("Predicates"))
969969
if (const SubtargetFeatureInfo *Feature =
970970
Info.getSubtargetFeature(Predicate))
971971
RequiredFeatures.push_back(Feature);
@@ -1215,7 +1215,7 @@ ClassInfo *AsmMatcherInfo::getOperandClass(const Record *Rec, int SubOpIdx) {
12151215
}
12161216

12171217
// No custom match class. Just use the register class.
1218-
Record *ClassRec = Rec->getValueAsDef("RegClass");
1218+
const Record *ClassRec = Rec->getValueAsDef("RegClass");
12191219
if (!ClassRec)
12201220
PrintFatalError(Rec->getLoc(),
12211221
"RegisterOperand `" + Rec->getName() +
@@ -1235,7 +1235,7 @@ ClassInfo *AsmMatcherInfo::getOperandClass(const Record *Rec, int SubOpIdx) {
12351235
PrintFatalError(Rec->getLoc(),
12361236
"Operand `" + Rec->getName() +
12371237
"' does not derive from class Operand!\n");
1238-
Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
1238+
const Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
12391239
if (ClassInfo *CI = AsmOperandClasses[MatchClass])
12401240
return CI;
12411241

@@ -1384,17 +1384,17 @@ void AsmMatcherInfo::buildRegisterClasses(
13841384
}
13851385

13861386
void AsmMatcherInfo::buildOperandClasses() {
1387-
std::vector<Record *> AsmOperands =
1387+
ArrayRef<const Record *> AsmOperands =
13881388
Records.getAllDerivedDefinitions("AsmOperandClass");
13891389

13901390
// Pre-populate AsmOperandClasses map.
1391-
for (Record *Rec : AsmOperands) {
1391+
for (const Record *Rec : AsmOperands) {
13921392
Classes.emplace_front();
13931393
AsmOperandClasses[Rec] = &Classes.front();
13941394
}
13951395

13961396
unsigned Index = 0;
1397-
for (Record *Rec : AsmOperands) {
1397+
for (const Record *Rec : AsmOperands) {
13981398
ClassInfo *CI = AsmOperandClasses[Rec];
13991399
CI->Kind = ClassInfo::UserClass0 + Index;
14001400

@@ -1468,14 +1468,14 @@ void AsmMatcherInfo::buildOperandClasses() {
14681468
}
14691469
}
14701470

1471-
AsmMatcherInfo::AsmMatcherInfo(Record *asmParser, CodeGenTarget &target,
1472-
RecordKeeper &records)
1471+
AsmMatcherInfo::AsmMatcherInfo(const Record *asmParser,
1472+
const CodeGenTarget &target,
1473+
const RecordKeeper &records)
14731474
: Records(records), AsmParser(asmParser), Target(target) {}
14741475

14751476
/// buildOperandMatchInfo - Build the necessary information to handle user
14761477
/// defined operand parsing methods.
14771478
void AsmMatcherInfo::buildOperandMatchInfo() {
1478-
14791479
/// Map containing a mask with all operands indices that can be found for
14801480
/// that class inside a instruction.
14811481
typedef std::map<ClassInfo *, unsigned, deref<std::less<>>> OpClassMaskTy;
@@ -1527,7 +1527,7 @@ void AsmMatcherInfo::buildInfo() {
15271527
SmallPtrSet<const Record *, 16> SingletonRegisters;
15281528
unsigned VariantCount = Target.getAsmParserVariantCount();
15291529
for (unsigned VC = 0; VC != VariantCount; ++VC) {
1530-
Record *AsmVariant = Target.getAsmParserVariant(VC);
1530+
const Record *AsmVariant = Target.getAsmParserVariant(VC);
15311531
StringRef CommentDelimiter =
15321532
AsmVariant->getValueAsString("CommentDelimiter");
15331533
AsmVariantInfo Variant;
@@ -1570,9 +1570,8 @@ void AsmMatcherInfo::buildInfo() {
15701570

15711571
// Parse all of the InstAlias definitions and stick them in the list of
15721572
// matchables.
1573-
std::vector<Record *> AllInstAliases =
1574-
Records.getAllDerivedDefinitions("InstAlias");
1575-
for (Record *InstAlias : AllInstAliases) {
1573+
for (const Record *InstAlias :
1574+
Records.getAllDerivedDefinitions("InstAlias")) {
15761575
auto Alias = std::make_unique<CodeGenInstAlias>(InstAlias, Target);
15771576

15781577
// If the tblgen -match-prefix option is specified (for tblgen hackers),
@@ -1678,9 +1677,7 @@ void AsmMatcherInfo::buildInfo() {
16781677

16791678
// Process token alias definitions and set up the associated superclass
16801679
// information.
1681-
std::vector<Record *> AllTokenAliases =
1682-
Records.getAllDerivedDefinitions("TokenAlias");
1683-
for (Record *Rec : AllTokenAliases) {
1680+
for (const Record *Rec : Records.getAllDerivedDefinitions("TokenAlias")) {
16841681
ClassInfo *FromClass = getTokenClass(Rec->getValueAsString("FromToken"));
16851682
ClassInfo *ToClass = getTokenClass(Rec->getValueAsString("ToToken"));
16861683
if (FromClass == ToClass)
@@ -2616,8 +2613,8 @@ static void emitMatchTokenString(CodeGenTarget &Target,
26162613

26172614
/// emitMatchRegisterName - Emit the function to match a string to the target
26182615
/// specific register enum.
2619-
static void emitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser,
2620-
raw_ostream &OS) {
2616+
static void emitMatchRegisterName(const CodeGenTarget &Target,
2617+
const Record *AsmParser, raw_ostream &OS) {
26212618
// Construct the match list.
26222619
std::vector<StringMatcher::StringPair> Matches;
26232620
const auto &Regs = Target.getRegBank().getRegisters();
@@ -2644,8 +2641,8 @@ static void emitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser,
26442641

26452642
/// Emit the function to match a string to the target
26462643
/// specific register enum.
2647-
static void emitMatchRegisterAltName(CodeGenTarget &Target, Record *AsmParser,
2648-
raw_ostream &OS) {
2644+
static void emitMatchRegisterAltName(const CodeGenTarget &Target,
2645+
const Record *AsmParser, raw_ostream &OS) {
26492646
// Construct the match list.
26502647
std::vector<StringMatcher::StringPair> Matches;
26512648
const auto &Regs = Target.getRegBank().getRegisters();
@@ -2744,13 +2741,13 @@ static std::string GetAliasRequiredFeatures(const Record *R,
27442741

27452742
static void
27462743
emitMnemonicAliasVariant(raw_ostream &OS, const AsmMatcherInfo &Info,
2747-
std::vector<Record *> &Aliases, unsigned Indent = 0,
2744+
ArrayRef<const Record *> Aliases, unsigned Indent = 0,
27482745
StringRef AsmParserVariantName = StringRef()) {
27492746
// Keep track of all the aliases from a mnemonic. Use an std::map so that the
27502747
// iteration order of the map is stable.
2751-
std::map<std::string, std::vector<Record *>> AliasesFromMnemonic;
2748+
std::map<std::string, std::vector<const Record *>> AliasesFromMnemonic;
27522749

2753-
for (Record *R : Aliases) {
2750+
for (const Record *R : Aliases) {
27542751
// FIXME: Allow AssemblerVariantName to be a comma separated list.
27552752
StringRef AsmVariantName = R->getValueAsString("AsmVariantName");
27562753
if (AsmVariantName != AsmParserVariantName)
@@ -2827,7 +2824,7 @@ static bool emitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info,
28272824
if (!MatchPrefix.empty())
28282825
return false;
28292826

2830-
std::vector<Record *> Aliases =
2827+
ArrayRef<const Record *> Aliases =
28312828
Info.getRecords().getAllDerivedDefinitions("MnemonicAlias");
28322829
if (Aliases.empty())
28332830
return false;
@@ -2836,7 +2833,7 @@ static bool emitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info,
28362833
"const FeatureBitset &Features, unsigned VariantID) {\n";
28372834
unsigned VariantCount = Target.getAsmParserVariantCount();
28382835
for (unsigned VC = 0; VC != VariantCount; ++VC) {
2839-
Record *AsmVariant = Target.getAsmParserVariant(VC);
2836+
const Record *AsmVariant = Target.getAsmParserVariant(VC);
28402837
int AsmParserVariantNo = AsmVariant->getValueAsInt("Variant");
28412838
StringRef AsmParserVariantName = AsmVariant->getValueAsString("Name");
28422839

@@ -3104,7 +3101,7 @@ static void emitMnemonicSpellChecker(raw_ostream &OS, CodeGenTarget &Target,
31043101
OS << " switch (VariantID) {\n";
31053102
OS << " default: llvm_unreachable(\"invalid variant!\");\n";
31063103
for (unsigned VC = 0; VC != VariantCount; ++VC) {
3107-
Record *AsmVariant = Target.getAsmParserVariant(VC);
3104+
const Record *AsmVariant = Target.getAsmParserVariant(VC);
31083105
int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
31093106
OS << " case " << AsmVariantNo << ": Start = std::begin(MatchTable" << VC
31103107
<< "); End = std::end(MatchTable" << VC << "); break;\n";
@@ -3164,7 +3161,7 @@ static void emitMnemonicChecker(raw_ostream &OS, CodeGenTarget &Target,
31643161
OS << " switch (VariantID) {\n";
31653162
OS << " default: llvm_unreachable(\"invalid variant!\");\n";
31663163
for (unsigned VC = 0; VC != VariantCount; ++VC) {
3167-
Record *AsmVariant = Target.getAsmParserVariant(VC);
3164+
const Record *AsmVariant = Target.getAsmParserVariant(VC);
31683165
int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
31693166
OS << " case " << AsmVariantNo << ": Start = std::begin(MatchTable" << VC
31703167
<< "); End = std::end(MatchTable" << VC << "); break;\n";
@@ -3231,7 +3228,7 @@ getNameForFeatureBitset(ArrayRef<const Record *> FeatureBitset) {
32313228

32323229
void AsmMatcherEmitter::run(raw_ostream &OS) {
32333230
CodeGenTarget Target(Records);
3234-
Record *AsmParser = Target.getAsmParser();
3231+
const Record *AsmParser = Target.getAsmParser();
32353232
StringRef ClassName = AsmParser->getValueAsString("AsmParserClassName");
32363233

32373234
emitSourceFileHeader("Assembly Matcher Source Fragment", OS, Records);
@@ -3536,7 +3533,7 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
35363533

35373534
unsigned VariantCount = Target.getAsmParserVariantCount();
35383535
for (unsigned VC = 0; VC != VariantCount; ++VC) {
3539-
Record *AsmVariant = Target.getAsmParserVariant(VC);
3536+
const Record *AsmVariant = Target.getAsmParserVariant(VC);
35403537
int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
35413538

35423539
OS << "static const MatchEntry MatchTable" << VC << "[] = {\n";
@@ -3637,7 +3634,7 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
36373634
OS << " switch (VariantID) {\n";
36383635
OS << " default: llvm_unreachable(\"invalid variant!\");\n";
36393636
for (unsigned VC = 0; VC != VariantCount; ++VC) {
3640-
Record *AsmVariant = Target.getAsmParserVariant(VC);
3637+
const Record *AsmVariant = Target.getAsmParserVariant(VC);
36413638
int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
36423639
OS << " case " << AsmVariantNo << ": Start = std::begin(MatchTable" << VC
36433640
<< "); End = std::end(MatchTable" << VC << "); break;\n";

0 commit comments

Comments
 (0)