Skip to content

Commit 2c96670

Browse files
authored
[LLVM][TableGen] Change CompressInstEmitter to use const RecordKeeper (#109035)
Change CompressInstEmitter 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 80aa4da commit 2c96670

File tree

1 file changed

+47
-45
lines changed

1 file changed

+47
-45
lines changed

llvm/utils/TableGen/CompressInstEmitter.cpp

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class CompressInstEmitter {
9292
// Integer immediate value.
9393
int64_t Imm;
9494
// Physical register.
95-
Record *Reg;
95+
const Record *Reg;
9696
} Data;
9797
// Tied operand index within the instruction.
9898
int TiedOpIdx = -1;
@@ -103,7 +103,7 @@ class CompressInstEmitter {
103103
// The destination instruction to transform to.
104104
CodeGenInstruction Dest;
105105
// Required target features to enable pattern.
106-
std::vector<Record *> PatReqFeatures;
106+
std::vector<const Record *> PatReqFeatures;
107107
// Maps operands in the Source Instruction to
108108
// the corresponding Dest instruction operand.
109109
IndexedMap<OpData> SourceOperandMap;
@@ -112,38 +112,40 @@ class CompressInstEmitter {
112112
IndexedMap<OpData> DestOperandMap;
113113

114114
bool IsCompressOnly;
115-
CompressPat(CodeGenInstruction &S, CodeGenInstruction &D,
116-
std::vector<Record *> RF, IndexedMap<OpData> &SourceMap,
115+
CompressPat(const CodeGenInstruction &S, const CodeGenInstruction &D,
116+
std::vector<const Record *> RF, IndexedMap<OpData> &SourceMap,
117117
IndexedMap<OpData> &DestMap, bool IsCompressOnly)
118118
: Source(S), Dest(D), PatReqFeatures(RF), SourceOperandMap(SourceMap),
119119
DestOperandMap(DestMap), IsCompressOnly(IsCompressOnly) {}
120120
};
121121
enum EmitterType { Compress, Uncompress, CheckCompress };
122-
RecordKeeper &Records;
123-
CodeGenTarget Target;
122+
const RecordKeeper &Records;
123+
const CodeGenTarget Target;
124124
SmallVector<CompressPat, 4> CompressPatterns;
125125

126-
void addDagOperandMapping(Record *Rec, DagInit *Dag, CodeGenInstruction &Inst,
126+
void addDagOperandMapping(const Record *Rec, const DagInit *Dag,
127+
const CodeGenInstruction &Inst,
127128
IndexedMap<OpData> &OperandMap, bool IsSourceInst);
128-
void evaluateCompressPat(Record *Compress);
129+
void evaluateCompressPat(const Record *Compress);
129130
void emitCompressInstEmitter(raw_ostream &OS, EmitterType EType);
130131
bool validateTypes(const Record *DagOpType, const Record *InstOpType,
131132
bool IsSourceInst);
132133
bool validateRegister(const Record *Reg, const Record *RegClass);
133-
void createDagOperandMapping(Record *Rec, StringMap<unsigned> &SourceOperands,
134+
void createDagOperandMapping(const Record *Rec,
135+
StringMap<unsigned> &SourceOperands,
134136
StringMap<unsigned> &DestOperands,
135-
DagInit *SourceDag, DagInit *DestDag,
137+
const DagInit *SourceDag, const DagInit *DestDag,
136138
IndexedMap<OpData> &SourceOperandMap);
137139

138-
void createInstOperandMapping(Record *Rec, DagInit *SourceDag,
139-
DagInit *DestDag,
140+
void createInstOperandMapping(const Record *Rec, const DagInit *SourceDag,
141+
const DagInit *DestDag,
140142
IndexedMap<OpData> &SourceOperandMap,
141143
IndexedMap<OpData> &DestOperandMap,
142144
StringMap<unsigned> &SourceOperands,
143-
CodeGenInstruction &DestInst);
145+
const CodeGenInstruction &DestInst);
144146

145147
public:
146-
CompressInstEmitter(RecordKeeper &R) : Records(R), Target(R) {}
148+
CompressInstEmitter(const RecordKeeper &R) : Records(R), Target(R) {}
147149

148150
void run(raw_ostream &OS);
149151
};
@@ -156,7 +158,7 @@ bool CompressInstEmitter::validateRegister(const Record *Reg,
156158
"RegClass record should be a RegisterClass");
157159
const CodeGenRegisterClass &RC = Target.getRegisterClass(RegClass);
158160
const CodeGenRegister *R = Target.getRegisterByName(Reg->getName().lower());
159-
assert((R != nullptr) && "Register not defined!!");
161+
assert(R != nullptr && "Register not defined!!");
160162
return RC.contains(R);
161163
}
162164

@@ -199,8 +201,9 @@ bool CompressInstEmitter::validateTypes(const Record *DagOpType,
199201
/// operands and fixed registers it expects the Dag operand type to be contained
200202
/// in the instantiated instruction operand type. For immediate operands and
201203
/// immediates no validation checks are enforced at pattern validation time.
202-
void CompressInstEmitter::addDagOperandMapping(Record *Rec, DagInit *Dag,
203-
CodeGenInstruction &Inst,
204+
void CompressInstEmitter::addDagOperandMapping(const Record *Rec,
205+
const DagInit *Dag,
206+
const CodeGenInstruction &Inst,
204207
IndexedMap<OpData> &OperandMap,
205208
bool IsSourceInst) {
206209
// TiedCount keeps track of the number of operands skipped in Inst
@@ -218,7 +221,7 @@ void CompressInstEmitter::addDagOperandMapping(Record *Rec, DagInit *Dag,
218221
TiedCount++;
219222
continue;
220223
}
221-
if (DefInit *DI = dyn_cast<DefInit>(Dag->getArg(I - TiedCount))) {
224+
if (const DefInit *DI = dyn_cast<DefInit>(Dag->getArg(I - TiedCount))) {
222225
if (DI->getDef()->isSubClassOf("Register")) {
223226
// Check if the fixed register belongs to the Register class.
224227
if (!validateRegister(DI->getDef(), Inst.Operands[I].Rec))
@@ -267,7 +270,7 @@ void CompressInstEmitter::addDagOperandMapping(Record *Rec, DagInit *Dag,
267270
}
268271

269272
// Verify the Dag operand count is enough to build an instruction.
270-
static bool verifyDagOpCount(CodeGenInstruction &Inst, DagInit *Dag,
273+
static bool verifyDagOpCount(const CodeGenInstruction &Inst, const DagInit *Dag,
271274
bool IsSource) {
272275
if (Dag->getNumArgs() == Inst.Operands.size())
273276
return true;
@@ -297,7 +300,7 @@ static bool verifyDagOpCount(CodeGenInstruction &Inst, DagInit *Dag,
297300
return true;
298301
}
299302

300-
static bool validateArgsTypes(Init *Arg1, Init *Arg2) {
303+
static bool validateArgsTypes(const Init *Arg1, const Init *Arg2) {
301304
return cast<DefInit>(Arg1)->getDef() == cast<DefInit>(Arg2)->getDef();
302305
}
303306

@@ -307,9 +310,9 @@ static bool validateArgsTypes(Init *Arg1, Init *Arg2) {
307310
// mapping $rs1 --> 0, $rs2 ---> 1. If the operand appears twice in the (tied)
308311
// same Dag we use the last occurrence for indexing.
309312
void CompressInstEmitter::createDagOperandMapping(
310-
Record *Rec, StringMap<unsigned> &SourceOperands,
311-
StringMap<unsigned> &DestOperands, DagInit *SourceDag, DagInit *DestDag,
312-
IndexedMap<OpData> &SourceOperandMap) {
313+
const Record *Rec, StringMap<unsigned> &SourceOperands,
314+
StringMap<unsigned> &DestOperands, const DagInit *SourceDag,
315+
const DagInit *DestDag, IndexedMap<OpData> &SourceOperandMap) {
313316
for (unsigned I = 0; I < DestDag->getNumArgs(); ++I) {
314317
// Skip fixed immediates and registers, they were handled in
315318
// addDagOperandMapping.
@@ -354,9 +357,9 @@ void CompressInstEmitter::createDagOperandMapping(
354357
/// output instructions. Validate that operands defined in the input are
355358
/// used in the output pattern while populating the maps.
356359
void CompressInstEmitter::createInstOperandMapping(
357-
Record *Rec, DagInit *SourceDag, DagInit *DestDag,
360+
const Record *Rec, const DagInit *SourceDag, const DagInit *DestDag,
358361
IndexedMap<OpData> &SourceOperandMap, IndexedMap<OpData> &DestOperandMap,
359-
StringMap<unsigned> &SourceOperands, CodeGenInstruction &DestInst) {
362+
StringMap<unsigned> &SourceOperands, const CodeGenInstruction &DestInst) {
360363
// TiedCount keeps track of the number of operands skipped in Inst
361364
// operands list to get to the corresponding Dag operand.
362365
unsigned TiedCount = 0;
@@ -423,14 +426,14 @@ void CompressInstEmitter::createInstOperandMapping(
423426
/// and generate warning.
424427
/// - Immediate operand type in Dag Input differs from the corresponding Source
425428
/// Instruction type and generate a warning.
426-
void CompressInstEmitter::evaluateCompressPat(Record *Rec) {
429+
void CompressInstEmitter::evaluateCompressPat(const Record *Rec) {
427430
// Validate input Dag operands.
428431
DagInit *SourceDag = Rec->getValueAsDag("Input");
429432
assert(SourceDag && "Missing 'Input' in compress pattern!");
430433
LLVM_DEBUG(dbgs() << "Input: " << *SourceDag << "\n");
431434

432435
// Checking we are transforming from compressed to uncompressed instructions.
433-
Record *SourceOperator = SourceDag->getOperatorAsDef(Rec->getLoc());
436+
const Record *SourceOperator = SourceDag->getOperatorAsDef(Rec->getLoc());
434437
CodeGenInstruction SourceInst(SourceOperator);
435438
verifyDagOpCount(SourceInst, SourceDag, true);
436439

@@ -439,7 +442,7 @@ void CompressInstEmitter::evaluateCompressPat(Record *Rec) {
439442
assert(DestDag && "Missing 'Output' in compress pattern!");
440443
LLVM_DEBUG(dbgs() << "Output: " << *DestDag << "\n");
441444

442-
Record *DestOperator = DestDag->getOperatorAsDef(Rec->getLoc());
445+
const Record *DestOperator = DestDag->getOperatorAsDef(Rec->getLoc());
443446
CodeGenInstruction DestInst(DestOperator);
444447
verifyDagOpCount(DestInst, DestDag, false);
445448

@@ -475,9 +478,9 @@ void CompressInstEmitter::evaluateCompressPat(Record *Rec) {
475478
DestOperandMap, SourceOperands, DestInst);
476479

477480
// Get the target features for the CompressPat.
478-
std::vector<Record *> PatReqFeatures;
479-
std::vector<Record *> RF = Rec->getValueAsListOfDefs("Predicates");
480-
copy_if(RF, std::back_inserter(PatReqFeatures), [](Record *R) {
481+
std::vector<const Record *> PatReqFeatures;
482+
std::vector<const Record *> RF = Rec->getValueAsListOfConstDefs("Predicates");
483+
copy_if(RF, std::back_inserter(PatReqFeatures), [](const Record *R) {
481484
return R->getValueAsBit("AssemblerMatcherPredicate");
482485
});
483486

@@ -489,8 +492,8 @@ void CompressInstEmitter::evaluateCompressPat(Record *Rec) {
489492
static void
490493
getReqFeatures(std::set<std::pair<bool, StringRef>> &FeaturesSet,
491494
std::set<std::set<std::pair<bool, StringRef>>> &AnyOfFeatureSets,
492-
const std::vector<Record *> &ReqFeatures) {
493-
for (auto &R : ReqFeatures) {
495+
ArrayRef<const Record *> ReqFeatures) {
496+
for (const Record *R : ReqFeatures) {
494497
const DagInit *D = R->getValueAsDag("AssemblerCondDag");
495498
std::string CombineType = D->getOperator()->getAsString();
496499
if (CombineType != "any_of" && CombineType != "all_of")
@@ -542,8 +545,8 @@ static unsigned getPredicates(DenseMap<const Record *, unsigned> &PredicateMap,
542545
return 0;
543546
}
544547

545-
static void printPredicates(const std::vector<const Record *> &Predicates,
546-
StringRef Name, raw_ostream &OS) {
548+
static void printPredicates(ArrayRef<const Record *> Predicates, StringRef Name,
549+
raw_ostream &OS) {
547550
for (unsigned I = 0; I < Predicates.size(); ++I) {
548551
StringRef Pred = Predicates[I]->getValueAsString(Name);
549552
OS << " case " << I + 1 << ": {\n"
@@ -565,7 +568,7 @@ static void mergeCondAndCode(raw_ostream &CombinedStream, StringRef CondStr,
565568

566569
void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &OS,
567570
EmitterType EType) {
568-
Record *AsmWriter = Target.getAsmWriter();
571+
const Record *AsmWriter = Target.getAsmWriter();
569572
if (!AsmWriter->getValueAsInt("PassSubtarget"))
570573
PrintFatalError(AsmWriter->getLoc(),
571574
"'PassSubtarget' is false. SubTargetInfo object is needed "
@@ -683,9 +686,10 @@ void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &OS,
683686
getReqFeatures(FeaturesSet, AnyOfFeatureSets, CompressPat.PatReqFeatures);
684687

685688
// Add Dest instruction required features.
686-
std::vector<Record *> ReqFeatures;
687-
std::vector<Record *> RF = Dest.TheDef->getValueAsListOfDefs("Predicates");
688-
copy_if(RF, std::back_inserter(ReqFeatures), [](Record *R) {
689+
std::vector<const Record *> ReqFeatures;
690+
std::vector<const Record *> RF =
691+
Dest.TheDef->getValueAsListOfConstDefs("Predicates");
692+
copy_if(RF, std::back_inserter(ReqFeatures), [](const Record *R) {
689693
return R->getValueAsBit("AssemblerMatcherPredicate");
690694
});
691695
getReqFeatures(FeaturesSet, AnyOfFeatureSets, ReqFeatures);
@@ -738,7 +742,7 @@ void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &OS,
738742
<< ").getImm() == " << SourceOperandMap[OpNo].Data.Imm << ") &&\n";
739743
break;
740744
case OpData::Reg: {
741-
Record *Reg = SourceOperandMap[OpNo].Data.Reg;
745+
const Record *Reg = SourceOperandMap[OpNo].Data.Reg;
742746
CondStream.indent(6)
743747
<< "(MI.getOperand(" << OpNo << ").isReg()) &&\n"
744748
<< " (MI.getOperand(" << OpNo << ").getReg() == " << TargetName
@@ -827,7 +831,7 @@ void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &OS,
827831
case OpData::Reg: {
828832
if (CompressOrUncompress) {
829833
// Fixed register has been validated at pattern validation time.
830-
Record *Reg = DestOperandMap[OpNo].Data.Reg;
834+
const Record *Reg = DestOperandMap[OpNo].Data.Reg;
831835
CodeStream.indent(6)
832836
<< "OutInst.addOperand(MCOperand::createReg(" << TargetName
833837
<< "::" << Reg->getName() << "));\n";
@@ -891,11 +895,9 @@ void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &OS,
891895
}
892896

893897
void CompressInstEmitter::run(raw_ostream &OS) {
894-
std::vector<Record *> Insts = Records.getAllDerivedDefinitions("CompressPat");
895-
896898
// Process the CompressPat definitions, validating them as we do so.
897-
for (unsigned I = 0, E = Insts.size(); I != E; ++I)
898-
evaluateCompressPat(Insts[I]);
899+
for (const Record *Pat : Records.getAllDerivedDefinitions("CompressPat"))
900+
evaluateCompressPat(Pat);
899901

900902
// Emit file header.
901903
emitSourceFileHeader("Compress instruction Source Fragment", OS, Records);

0 commit comments

Comments
 (0)