Skip to content

Commit 3786568

Browse files
authored
[TableGen] Change CodeGenInstruction record members to const (#107921)
Change CodeGenInstruction::{TheDef, InfereredFrom} to const pointers. 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 7858e14 commit 3786568

19 files changed

+110
-109
lines changed

llvm/utils/TableGen/AsmMatcherEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ struct MatchableInfo {
504504

505505
/// TheDef - This is the definition of the instruction or InstAlias that this
506506
/// matchable came from.
507-
Record *const TheDef;
507+
const Record *const TheDef;
508508

509509
// ResInstSize - The size of the resulting instruction for this matchable.
510510
unsigned ResInstSize;

llvm/utils/TableGen/CodeEmitterGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ void CodeEmitterGen::emitInstructionBaseValues(
403403
<< HWM.getModeName(HwMode, /*IncludeDefault=*/true) << "[] = {\n";
404404

405405
for (const CodeGenInstruction *CGI : NumberedInstructions) {
406-
Record *R = CGI->TheDef;
406+
const Record *R = CGI->TheDef;
407407

408408
if (R->getValueAsString("Namespace") == "TargetOpcode" ||
409409
R->getValueAsBit("isPseudo")) {
@@ -485,7 +485,7 @@ void CodeEmitterGen::run(raw_ostream &o) {
485485
std::set<unsigned> HwModes;
486486
BitWidth = 0;
487487
for (const CodeGenInstruction *CGI : NumberedInstructions) {
488-
Record *R = CGI->TheDef;
488+
const Record *R = CGI->TheDef;
489489
if (R->getValueAsString("Namespace") == "TargetOpcode" ||
490490
R->getValueAsBit("isPseudo"))
491491
continue;

llvm/utils/TableGen/CodeGenMapTable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class MapTableEmitter {
185185

186186
// KeyInstrVec - list of key instructions.
187187
std::vector<Record *> KeyInstrVec;
188-
DenseMap<Record *, std::vector<Record *>> MapTable;
188+
DenseMap<const Record *, std::vector<Record *>> MapTable;
189189

190190
public:
191191
MapTableEmitter(CodeGenTarget &Target, RecordKeeper &Records, Record *IMRec)
@@ -371,7 +371,7 @@ unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) {
371371
// emitted as first column.
372372
OS << "Table[][" << NumCol + 1 << "] = {\n";
373373
for (unsigned i = 0; i < TotalNumInstr; i++) {
374-
Record *CurInstr = NumberedInstructions[i]->TheDef;
374+
const Record *CurInstr = NumberedInstructions[i]->TheDef;
375375
std::vector<Record *> ColInstrs = MapTable[CurInstr];
376376
std::string OutStr;
377377
unsigned RelExists = 0;

llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,28 +1255,28 @@ bool TreePredicateFn::isAtomicOrderingWeakerThanRelease() const {
12551255
false);
12561256
}
12571257
Record *TreePredicateFn::getMemoryVT() const {
1258-
Record *R = getOrigPatFragRecord()->getRecord();
1258+
const Record *R = getOrigPatFragRecord()->getRecord();
12591259
if (R->isValueUnset("MemoryVT"))
12601260
return nullptr;
12611261
return R->getValueAsDef("MemoryVT");
12621262
}
12631263

12641264
ListInit *TreePredicateFn::getAddressSpaces() const {
1265-
Record *R = getOrigPatFragRecord()->getRecord();
1265+
const Record *R = getOrigPatFragRecord()->getRecord();
12661266
if (R->isValueUnset("AddressSpaces"))
12671267
return nullptr;
12681268
return R->getValueAsListInit("AddressSpaces");
12691269
}
12701270

12711271
int64_t TreePredicateFn::getMinAlignment() const {
1272-
Record *R = getOrigPatFragRecord()->getRecord();
1272+
const Record *R = getOrigPatFragRecord()->getRecord();
12731273
if (R->isValueUnset("MinAlignment"))
12741274
return 0;
12751275
return R->getValueAsInt("MinAlignment");
12761276
}
12771277

12781278
Record *TreePredicateFn::getScalarMemoryVT() const {
1279-
Record *R = getOrigPatFragRecord()->getRecord();
1279+
const Record *R = getOrigPatFragRecord()->getRecord();
12801280
if (R->isValueUnset("ScalarMemoryVT"))
12811281
return nullptr;
12821282
return R->getValueAsDef("ScalarMemoryVT");
@@ -1390,7 +1390,7 @@ std::string TreePredicateFn::getCodeToRunOnSDNode() const {
13901390
if (Tree->isLeaf())
13911391
TreeClassName = "SDNode";
13921392
else {
1393-
Record *Op = Tree->getOperator();
1393+
const Record *Op = Tree->getOperator();
13941394
const SDNodeInfo &Info = PatFragRec->getDAGPatterns().getSDNodeInfo(Op);
13951395
TreeClassName = Info.getSDClassName();
13961396
}
@@ -1848,7 +1848,8 @@ MVT::SimpleValueType SDNodeInfo::getKnownType(unsigned ResNo) const {
18481848
// TreePatternNode implementation
18491849
//
18501850

1851-
static unsigned GetNumNodeResults(Record *Operator, CodeGenDAGPatterns &CDP) {
1851+
static unsigned GetNumNodeResults(const Record *Operator,
1852+
CodeGenDAGPatterns &CDP) {
18521853
if (Operator->getName() == "set" || Operator->getName() == "implicit")
18531854
return 0; // All return nothing.
18541855

@@ -2077,7 +2078,7 @@ void TreePatternNode::InlinePatternFragments(
20772078
return;
20782079
}
20792080

2080-
Record *Op = getOperator();
2081+
const Record *Op = getOperator();
20812082

20822083
if (!Op->isSubClassOf("PatFrags")) {
20832084
if (getNumChildren() == 0) {
@@ -2340,7 +2341,7 @@ TreePatternNode::getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const {
23402341
/// return the ComplexPattern information, otherwise return null.
23412342
const ComplexPattern *
23422343
TreePatternNode::getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const {
2343-
Record *Rec;
2344+
const Record *Rec;
23442345
if (isLeaf()) {
23452346
DefInit *DI = dyn_cast<DefInit>(getLeafValue());
23462347
if (!DI)
@@ -2793,23 +2794,23 @@ bool TreePatternNode::canPatternMatch(std::string &Reason,
27932794
// TreePattern implementation
27942795
//
27952796

2796-
TreePattern::TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
2797+
TreePattern::TreePattern(const Record *TheRec, ListInit *RawPat, bool isInput,
27972798
CodeGenDAGPatterns &cdp)
27982799
: TheRecord(TheRec), CDP(cdp), isInputPattern(isInput), HasError(false),
27992800
Infer(*this) {
28002801
for (Init *I : RawPat->getValues())
28012802
Trees.push_back(ParseTreePattern(I, ""));
28022803
}
28032804

2804-
TreePattern::TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
2805+
TreePattern::TreePattern(const Record *TheRec, DagInit *Pat, bool isInput,
28052806
CodeGenDAGPatterns &cdp)
28062807
: TheRecord(TheRec), CDP(cdp), isInputPattern(isInput), HasError(false),
28072808
Infer(*this) {
28082809
Trees.push_back(ParseTreePattern(Pat, ""));
28092810
}
28102811

2811-
TreePattern::TreePattern(Record *TheRec, TreePatternNodePtr Pat, bool isInput,
2812-
CodeGenDAGPatterns &cdp)
2812+
TreePattern::TreePattern(const Record *TheRec, TreePatternNodePtr Pat,
2813+
bool isInput, CodeGenDAGPatterns &cdp)
28132814
: TheRecord(TheRec), CDP(cdp), isInputPattern(isInput), HasError(false),
28142815
Infer(*this) {
28152816
Trees.push_back(Pat);
@@ -3389,7 +3390,7 @@ static bool HandleUse(TreePattern &I, TreePatternNodePtr Pat,
33893390
return false;
33903391
}
33913392

3392-
Record *Rec;
3393+
const Record *Rec;
33933394
if (Pat->isLeaf()) {
33943395
DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue());
33953396
if (!DI)
@@ -3408,7 +3409,7 @@ static bool HandleUse(TreePattern &I, TreePatternNodePtr Pat,
34083409
Slot = Pat;
34093410
return true;
34103411
}
3411-
Record *SlotRec;
3412+
const Record *SlotRec;
34123413
if (Slot->isLeaf()) {
34133414
SlotRec = cast<DefInit>(Slot->getLeafValue())->getDef();
34143415
} else {
@@ -3633,7 +3634,8 @@ class InstAnalyzer {
36333634
};
36343635

36353636
static bool InferFromPattern(CodeGenInstruction &InstInfo,
3636-
const InstAnalyzer &PatInfo, Record *PatDef) {
3637+
const InstAnalyzer &PatInfo,
3638+
const Record *PatDef) {
36373639
bool Error = false;
36383640

36393641
// Remember where InstInfo got its flags.
@@ -3729,7 +3731,7 @@ static bool hasNullFragReference(ListInit *LI) {
37293731

37303732
/// Get all the instructions in a tree.
37313733
static void getInstructionsInTree(TreePatternNode &Tree,
3732-
SmallVectorImpl<Record *> &Instrs) {
3734+
SmallVectorImpl<const Record *> &Instrs) {
37333735
if (Tree.isLeaf())
37343736
return;
37353737
if (Tree.getOperator()->isSubClassOf("Instruction"))
@@ -3935,8 +3937,7 @@ void CodeGenDAGPatterns::parseInstructionPattern(CodeGenInstruction &CGI,
39353937

39363938
// Create and insert the instruction.
39373939
// FIXME: InstImpResults should not be part of DAGInstruction.
3938-
Record *R = I.getRecord();
3939-
DAGInsts.try_emplace(R, std::move(Results), std::move(Operands),
3940+
DAGInsts.try_emplace(I.getRecord(), std::move(Results), std::move(Operands),
39403941
std::move(InstImpResults), SrcPattern, ResultPattern);
39413942

39423943
LLVM_DEBUG(I.dump());
@@ -3989,9 +3990,7 @@ void CodeGenDAGPatterns::ParseInstructions() {
39893990
}
39903991

39913992
// If we can, convert the instructions to be patterns that are matched!
3992-
for (auto &Entry : Instructions) {
3993-
Record *Instr = Entry.first;
3994-
DAGInstruction &TheInst = Entry.second;
3993+
for (const auto &[Instr, TheInst] : Instructions) {
39953994
TreePatternNodePtr SrcPattern = TheInst.getSrcPattern();
39963995
TreePatternNodePtr ResultPattern = TheInst.getResultPattern();
39973996

@@ -4078,7 +4077,7 @@ void CodeGenDAGPatterns::InferInstructionFlags() {
40784077
for (const PatternToMatch &PTM : ptms()) {
40794078
// We can only infer from single-instruction patterns, otherwise we won't
40804079
// know which instruction should get the flags.
4081-
SmallVector<Record *, 8> PatInstrs;
4080+
SmallVector<const Record *, 8> PatInstrs;
40824081
getInstructionsInTree(PTM.getDstPattern(), PatInstrs);
40834082
if (PatInstrs.size() != 1)
40844083
continue;
@@ -4135,7 +4134,7 @@ void CodeGenDAGPatterns::InferInstructionFlags() {
41354134
void CodeGenDAGPatterns::VerifyInstructionFlags() {
41364135
unsigned Errors = 0;
41374136
for (const PatternToMatch &PTM : ptms()) {
4138-
SmallVector<Record *, 8> Instrs;
4137+
SmallVector<const Record *, 8> Instrs;
41394138
getInstructionsInTree(PTM.getDstPattern(), Instrs);
41404139
if (Instrs.empty())
41414140
continue;
@@ -4245,7 +4244,7 @@ static TreePatternNodePtr PromoteXForms(TreePatternNodePtr N) {
42454244
}
42464245

42474246
void CodeGenDAGPatterns::ParseOnePattern(
4248-
Record *TheDef, TreePattern &Pattern, TreePattern &Result,
4247+
const Record *TheDef, TreePattern &Pattern, TreePattern &Result,
42494248
const std::vector<Record *> &InstImpResults, bool ShouldIgnore) {
42504249

42514250
// Inline pattern fragments and expand multiple alternatives.
@@ -4591,7 +4590,7 @@ GatherChildrenOfAssociativeOpcode(TreePatternNodePtr N,
45914590
std::vector<TreePatternNodePtr> &Children) {
45924591
assert(N->getNumChildren() == 2 &&
45934592
"Associative but doesn't have 2 children!");
4594-
Record *Operator = N->getOperator();
4593+
const Record *Operator = N->getOperator();
45954594

45964595
// Only permit raw nodes.
45974596
if (!N->getName().empty() || !N->getPredicateCalls().empty() ||

0 commit comments

Comments
 (0)