Skip to content

Commit 3138eb5

Browse files
authored
[LLVM][TableGen] Use const record pointers in TableGen/Common files (#109467)
Use const record pointers in TableGen/Common files. 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 706e710 commit 3138eb5

12 files changed

+227
-232
lines changed

llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp

Lines changed: 73 additions & 85 deletions
Large diffs are not rendered by default.

llvm/utils/TableGen/Common/CodeGenDAGPatterns.h

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -582,13 +582,13 @@ class TreePredicateFn {
582582
/// If non-null, indicates that this predicate is a predefined memory VT
583583
/// predicate for a load/store and returns the ValueType record for the memory
584584
/// VT.
585-
Record *getMemoryVT() const;
585+
const Record *getMemoryVT() const;
586586
/// If non-null, indicates that this predicate is a predefined memory VT
587587
/// predicate (checking only the scalar type) for load/store and returns the
588588
/// ValueType record for the memory VT.
589-
Record *getScalarMemoryVT() const;
589+
const Record *getScalarMemoryVT() const;
590590

591-
ListInit *getAddressSpaces() const;
591+
const ListInit *getAddressSpaces() const;
592592
int64_t getMinAlignment() const;
593593

594594
// If true, indicates that GlobalISel-based C++ code was supplied.
@@ -634,7 +634,7 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
634634
/// OperatorOrVal - The Record for the operator if this is an interior node
635635
/// (not a leaf) or the init value (e.g. the "GPRC" record, or "7") for a
636636
/// leaf.
637-
PointerUnion<const Record *, Init *> OperatorOrVal;
637+
PointerUnion<const Record *, const Init *> OperatorOrVal;
638638

639639
/// Name - The name given to this node with the :$foo notation.
640640
///
@@ -648,7 +648,7 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
648648

649649
/// TransformFn - The transformation function to execute on this node before
650650
/// it can be substituted into the resulting instruction on a pattern match.
651-
Record *TransformFn;
651+
const Record *TransformFn;
652652

653653
std::vector<TreePatternNodePtr> Children;
654654

@@ -664,7 +664,7 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
664664
ResultPerm.resize(NumResults);
665665
std::iota(ResultPerm.begin(), ResultPerm.end(), 0);
666666
}
667-
TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
667+
TreePatternNode(const Init *val, unsigned NumResults) // leaf ctor
668668
: OperatorOrVal(val), TransformFn(nullptr) {
669669
Types.resize(NumResults);
670670
ResultPerm.resize(NumResults);
@@ -685,7 +685,7 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
685685
NamesAsPredicateArg.push_back(N);
686686
}
687687

688-
bool isLeaf() const { return isa<Init *>(OperatorOrVal); }
688+
bool isLeaf() const { return isa<const Init *>(OperatorOrVal); }
689689

690690
// Type accessors.
691691
unsigned getNumTypes() const { return Types.size(); }
@@ -713,9 +713,9 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
713713
unsigned getResultIndex(unsigned ResNo) const { return ResultPerm[ResNo]; }
714714
void setResultIndex(unsigned ResNo, unsigned RI) { ResultPerm[ResNo] = RI; }
715715

716-
Init *getLeafValue() const {
716+
const Init *getLeafValue() const {
717717
assert(isLeaf());
718-
return cast<Init *>(OperatorOrVal);
718+
return cast<const Init *>(OperatorOrVal);
719719
}
720720
const Record *getOperator() const {
721721
assert(!isLeaf());
@@ -766,8 +766,8 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
766766
addPredicateCall(TreePredicateCall(Fn, Scope));
767767
}
768768

769-
Record *getTransformFn() const { return TransformFn; }
770-
void setTransformFn(Record *Fn) { TransformFn = Fn; }
769+
const Record *getTransformFn() const { return TransformFn; }
770+
void setTransformFn(const Record *Fn) { TransformFn = Fn; }
771771

772772
/// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
773773
/// CodeGenIntrinsic information for it, otherwise return a null pointer.
@@ -901,14 +901,14 @@ class TreePattern {
901901
/// ComplexPattern. This records the ComplexPattern instance and the operand
902902
/// number for each operand encountered in a ComplexPattern to aid in that
903903
/// check.
904-
StringMap<std::pair<Record *, unsigned>> ComplexPatternOperands;
904+
StringMap<std::pair<const Record *, unsigned>> ComplexPatternOperands;
905905

906906
TypeInfer Infer;
907907

908908
public:
909909
/// TreePattern constructor - Parse the specified DagInits into the
910910
/// current record.
911-
TreePattern(const Record *TheRec, ListInit *RawPat, bool isInput,
911+
TreePattern(const Record *TheRec, const ListInit *RawPat, bool isInput,
912912
CodeGenDAGPatterns &ise);
913913
TreePattern(const Record *TheRec, DagInit *Pat, bool isInput,
914914
CodeGenDAGPatterns &ise);
@@ -1013,24 +1013,24 @@ struct DAGDefaultOperand {
10131013
class DAGInstruction {
10141014
std::vector<const Record *> Results;
10151015
std::vector<const Record *> Operands;
1016-
std::vector<Record *> ImpResults;
1016+
std::vector<const Record *> ImpResults;
10171017
TreePatternNodePtr SrcPattern;
10181018
TreePatternNodePtr ResultPattern;
10191019

10201020
public:
1021-
DAGInstruction(std::vector<const Record *> &&results,
1022-
std::vector<const Record *> &&operands,
1023-
std::vector<Record *> &&impresults,
1024-
TreePatternNodePtr srcpattern = nullptr,
1025-
TreePatternNodePtr resultpattern = nullptr)
1026-
: Results(std::move(results)), Operands(std::move(operands)),
1027-
ImpResults(std::move(impresults)), SrcPattern(srcpattern),
1028-
ResultPattern(resultpattern) {}
1021+
DAGInstruction(std::vector<const Record *> &&Results,
1022+
std::vector<const Record *> &&Operands,
1023+
std::vector<const Record *> &&ImpResults,
1024+
TreePatternNodePtr SrcPattern = nullptr,
1025+
TreePatternNodePtr ResultPattern = nullptr)
1026+
: Results(std::move(Results)), Operands(std::move(Operands)),
1027+
ImpResults(std::move(ImpResults)), SrcPattern(SrcPattern),
1028+
ResultPattern(ResultPattern) {}
10291029

10301030
unsigned getNumResults() const { return Results.size(); }
10311031
unsigned getNumOperands() const { return Operands.size(); }
10321032
unsigned getNumImpResults() const { return ImpResults.size(); }
1033-
const std::vector<Record *> &getImpResults() const { return ImpResults; }
1033+
ArrayRef<const Record *> getImpResults() const { return ImpResults; }
10341034

10351035
const Record *getResult(unsigned RN) const {
10361036
assert(RN < Results.size());
@@ -1042,7 +1042,7 @@ class DAGInstruction {
10421042
return Operands[ON];
10431043
}
10441044

1045-
Record *getImpResult(unsigned RN) const {
1045+
const Record *getImpResult(unsigned RN) const {
10461046
assert(RN < ImpResults.size());
10471047
return ImpResults[RN];
10481048
}
@@ -1058,7 +1058,7 @@ class PatternToMatch {
10581058
ListInit *Predicates; // Top level predicate conditions to match.
10591059
TreePatternNodePtr SrcPattern; // Source pattern to match.
10601060
TreePatternNodePtr DstPattern; // Resulting pattern.
1061-
std::vector<Record *> Dstregs; // Physical register defs being matched.
1061+
std::vector<const Record *> Dstregs; // Physical register defs being matched.
10621062
std::string HwModeFeatures;
10631063
int AddedComplexity; // Add to matching pattern complexity.
10641064
bool GISelShouldIgnore; // Should GlobalISel ignore importing this pattern.
@@ -1067,27 +1067,27 @@ class PatternToMatch {
10671067
public:
10681068
PatternToMatch(const Record *srcrecord, ListInit *preds,
10691069
TreePatternNodePtr src, TreePatternNodePtr dst,
1070-
std::vector<Record *> dstregs, int complexity, unsigned uid,
1070+
ArrayRef<const Record *> dstregs, int complexity, unsigned uid,
10711071
bool ignore, const Twine &hwmodefeatures = "")
10721072
: SrcRecord(srcrecord), Predicates(preds), SrcPattern(src),
1073-
DstPattern(dst), Dstregs(std::move(dstregs)),
1074-
HwModeFeatures(hwmodefeatures.str()), AddedComplexity(complexity),
1075-
GISelShouldIgnore(ignore), ID(uid) {}
1073+
DstPattern(dst), Dstregs(dstregs), HwModeFeatures(hwmodefeatures.str()),
1074+
AddedComplexity(complexity), GISelShouldIgnore(ignore), ID(uid) {}
10761075

10771076
const Record *getSrcRecord() const { return SrcRecord; }
10781077
ListInit *getPredicates() const { return Predicates; }
10791078
TreePatternNode &getSrcPattern() const { return *SrcPattern; }
10801079
TreePatternNodePtr getSrcPatternShared() const { return SrcPattern; }
10811080
TreePatternNode &getDstPattern() const { return *DstPattern; }
10821081
TreePatternNodePtr getDstPatternShared() const { return DstPattern; }
1083-
const std::vector<Record *> &getDstRegs() const { return Dstregs; }
1082+
ArrayRef<const Record *> getDstRegs() const { return Dstregs; }
10841083
StringRef getHwModeFeatures() const { return HwModeFeatures; }
10851084
int getAddedComplexity() const { return AddedComplexity; }
10861085
bool getGISelShouldIgnore() const { return GISelShouldIgnore; }
10871086
unsigned getID() const { return ID; }
10881087

10891088
std::string getPredicateCheck() const;
1090-
void getPredicateRecords(SmallVectorImpl<Record *> &PredicateRecs) const;
1089+
void
1090+
getPredicateRecords(SmallVectorImpl<const Record *> &PredicateRecs) const;
10911091

10921092
/// Compute the complexity metric for the input pattern. This roughly
10931093
/// corresponds to the number of nodes that are covered.
@@ -1113,8 +1113,8 @@ class CodeGenDAGPatterns {
11131113
std::map<const Record *, DAGInstruction, LessRecordByID> Instructions;
11141114

11151115
// Specific SDNode definitions:
1116-
Record *intrinsic_void_sdnode;
1117-
Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
1116+
const Record *intrinsic_void_sdnode;
1117+
const Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
11181118

11191119
/// PatternsToMatch - All of the things we are matching on the DAG. The first
11201120
/// value is the pattern to match, the second pattern is the result to
@@ -1136,7 +1136,7 @@ class CodeGenDAGPatterns {
11361136
const CodeGenTarget &getTargetInfo() const { return Target; }
11371137
const TypeSetByHwMode &getLegalTypes() const { return LegalVTS; }
11381138

1139-
Record *getSDNodeNamed(StringRef Name) const;
1139+
const Record *getSDNodeNamed(StringRef Name) const;
11401140

11411141
const SDNodeInfo &getSDNodeInfo(const Record *R) const {
11421142
auto F = SDNodes.find(R);
@@ -1170,7 +1170,7 @@ class CodeGenDAGPatterns {
11701170
llvm_unreachable("Bad intrinsic ID!");
11711171
}
11721172

1173-
unsigned getIntrinsicID(Record *R) const {
1173+
unsigned getIntrinsicID(const Record *R) const {
11741174
for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
11751175
if (Intrinsics[i].TheDef == R)
11761176
return i;
@@ -1209,7 +1209,7 @@ class CodeGenDAGPatterns {
12091209

12101210
/// Parse the Pattern for an instruction, and insert the result in DAGInsts.
12111211
typedef std::map<const Record *, DAGInstruction, LessRecordByID> DAGInstMap;
1212-
void parseInstructionPattern(CodeGenInstruction &CGI, ListInit *Pattern,
1212+
void parseInstructionPattern(CodeGenInstruction &CGI, const ListInit *Pattern,
12131213
DAGInstMap &DAGInsts);
12141214

12151215
const DAGInstruction &getInstruction(const Record *R) const {
@@ -1218,11 +1218,13 @@ class CodeGenDAGPatterns {
12181218
return F->second;
12191219
}
12201220

1221-
Record *get_intrinsic_void_sdnode() const { return intrinsic_void_sdnode; }
1222-
Record *get_intrinsic_w_chain_sdnode() const {
1221+
const Record *get_intrinsic_void_sdnode() const {
1222+
return intrinsic_void_sdnode;
1223+
}
1224+
const Record *get_intrinsic_w_chain_sdnode() const {
12231225
return intrinsic_w_chain_sdnode;
12241226
}
1225-
Record *get_intrinsic_wo_chain_sdnode() const {
1227+
const Record *get_intrinsic_wo_chain_sdnode() const {
12261228
return intrinsic_wo_chain_sdnode;
12271229
}
12281230

@@ -1248,15 +1250,15 @@ class CodeGenDAGPatterns {
12481250

12491251
void ParseOnePattern(const Record *TheDef, TreePattern &Pattern,
12501252
TreePattern &Result,
1251-
const std::vector<Record *> &InstImpResults,
1253+
ArrayRef<const Record *> InstImpResults,
12521254
bool ShouldIgnore = false);
12531255
void AddPatternToMatch(TreePattern *Pattern, PatternToMatch &&PTM);
12541256
void FindPatternInputsAndOutputs(
12551257
TreePattern &I, TreePatternNodePtr Pat,
12561258
std::map<std::string, TreePatternNodePtr> &InstInputs,
12571259
MapVector<std::string, TreePatternNodePtr,
12581260
std::map<std::string, unsigned>> &InstResults,
1259-
std::vector<Record *> &InstImpResults);
1261+
std::vector<const Record *> &InstImpResults);
12601262
unsigned getNewUID();
12611263
};
12621264

llvm/utils/TableGen/Common/CodeGenHwModes.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ LLVM_DUMP_METHOD
3939
void HwMode::dump() const { dbgs() << Name << ": " << Features << '\n'; }
4040

4141
HwModeSelect::HwModeSelect(const Record *R, CodeGenHwModes &CGH) {
42-
std::vector<Record *> Modes = R->getValueAsListOfDefs("Modes");
43-
std::vector<Record *> Objects = R->getValueAsListOfDefs("Objects");
42+
std::vector<const Record *> Modes = R->getValueAsListOfConstDefs("Modes");
43+
std::vector<const Record *> Objects = R->getValueAsListOfConstDefs("Objects");
4444
if (Modes.size() != Objects.size()) {
4545
PrintError(
4646
R->getLoc(),
@@ -49,9 +49,9 @@ HwModeSelect::HwModeSelect(const Record *R, CodeGenHwModes &CGH) {
4949
"have the same size");
5050
report_fatal_error("error in target description.");
5151
}
52-
for (unsigned i = 0, e = Modes.size(); i != e; ++i) {
53-
unsigned ModeId = CGH.getHwModeId(Modes[i]);
54-
Items.push_back(std::pair(ModeId, Objects[i]));
52+
for (auto [Mode, Object] : zip_equal(Modes, Objects)) {
53+
unsigned ModeId = CGH.getHwModeId(Mode);
54+
Items.push_back(std::pair(ModeId, Object));
5555
}
5656
}
5757

llvm/utils/TableGen/Common/CodeGenInstAlias.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ bool CodeGenInstAlias::tryAliasOpMatch(const DagInit *Result,
3030
ArrayRef<SMLoc> Loc,
3131
const CodeGenTarget &T,
3232
ResultOperand &ResOp) {
33-
Init *Arg = Result->getArg(AliasOpNo);
34-
DefInit *ADI = dyn_cast<DefInit>(Arg);
35-
Record *ResultRecord = ADI ? ADI->getDef() : nullptr;
33+
const Init *Arg = Result->getArg(AliasOpNo);
34+
const DefInit *ADI = dyn_cast<DefInit>(Arg);
35+
const Record *ResultRecord = ADI ? ADI->getDef() : nullptr;
3636

3737
if (ADI && ADI->getDef() == InstOpRec) {
3838
// If the operand is a record, it must have a name, and the record type
@@ -102,12 +102,12 @@ bool CodeGenInstAlias::tryAliasOpMatch(const DagInit *Result,
102102
// throw TGError(Loc, "reg0 used for result that is not an "
103103
// "OptionalDefOperand!");
104104

105-
ResOp = ResultOperand(static_cast<Record *>(nullptr));
105+
ResOp = ResultOperand(nullptr);
106106
return true;
107107
}
108108

109109
// Literal integers.
110-
if (IntInit *II = dyn_cast<IntInit>(Arg)) {
110+
if (const IntInit *II = dyn_cast<IntInit>(Arg)) {
111111
if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
112112
return false;
113113
// Integer arguments can't have names.
@@ -119,17 +119,16 @@ bool CodeGenInstAlias::tryAliasOpMatch(const DagInit *Result,
119119
}
120120

121121
// Bits<n> (also used for 0bxx literals)
122-
if (BitsInit *BI = dyn_cast<BitsInit>(Arg)) {
122+
if (const BitsInit *BI = dyn_cast<BitsInit>(Arg)) {
123123
if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
124124
return false;
125125
if (!BI->isComplete())
126126
return false;
127127
// Convert the bits init to an integer and use that for the result.
128-
IntInit *II = dyn_cast_or_null<IntInit>(
129-
BI->convertInitializerTo(IntRecTy::get(BI->getRecordKeeper())));
130-
if (!II)
128+
std::optional<int64_t> Value = BI->convertInitializerToInt();
129+
if (!Value)
131130
return false;
132-
ResOp = ResultOperand(II->getValue());
131+
ResOp = ResultOperand(*Value);
133132
return true;
134133
}
135134

@@ -182,15 +181,15 @@ CodeGenInstAlias::CodeGenInstAlias(const Record *R, const CodeGenTarget &T)
182181

183182
// NameClass - If argument names are repeated, we need to verify they have
184183
// the same class.
185-
StringMap<Record *> NameClass;
184+
StringMap<const Record *> NameClass;
186185
for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
187-
DefInit *ADI = dyn_cast<DefInit>(Result->getArg(i));
186+
const DefInit *ADI = dyn_cast<DefInit>(Result->getArg(i));
188187
if (!ADI || !Result->getArgName(i))
189188
continue;
190189
// Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
191190
// $foo can exist multiple times in the result list, but it must have the
192191
// same type.
193-
Record *&Entry = NameClass[Result->getArgNameStr(i)];
192+
const Record *&Entry = NameClass[Result->getArgNameStr(i)];
194193
if (Entry && Entry != ADI->getDef())
195194
PrintFatalError(R->getLoc(), "result value $" + Result->getArgNameStr(i) +
196195
" is both " + Entry->getName() +
@@ -235,9 +234,9 @@ CodeGenInstAlias::CodeGenInstAlias(const Record *R, const CodeGenTarget &T)
235234

236235
// Otherwise, we need to match each of the suboperands individually.
237236
} else {
238-
DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
237+
const DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
239238
for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
240-
Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
239+
const Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
241240

242241
// Take care to instantiate each of the suboperands with the correct
243242
// nomenclature: $foo.bar
@@ -255,11 +254,11 @@ CodeGenInstAlias::CodeGenInstAlias(const Record *R, const CodeGenTarget &T)
255254
// If the argument did not match the instruction operand, and the operand
256255
// is composed of multiple suboperands, try matching the suboperands.
257256
if (NumSubOps > 1) {
258-
DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
257+
const DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
259258
for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
260259
if (AliasOpNo >= Result->getNumArgs())
261260
PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
262-
Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
261+
const Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
263262
if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false, R->getLoc(), T,
264263
ResOp)) {
265264
ResultOperands.push_back(ResOp);

llvm/utils/TableGen/Common/CodeGenInstAlias.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class CodeGenInstAlias {
5757
ResultOperand(std::string N, const Record *R)
5858
: Name(std::move(N)), R(R), Kind(K_Record) {}
5959
ResultOperand(int64_t I) : Imm(I), Kind(K_Imm) {}
60-
ResultOperand(Record *R) : R(R), Kind(K_Reg) {}
60+
ResultOperand(const Record *R) : R(R), Kind(K_Reg) {}
6161

6262
bool isRecord() const { return Kind == K_Record; }
6363
bool isImm() const { return Kind == K_Imm; }

0 commit comments

Comments
 (0)