Skip to content

Commit 4b7cabf

Browse files
committedOct 8, 2019
[tblgen] Add getOperatorAsDef() to Record
Summary: While working with DagInit's, it's often the case that you expect the operator to be a reference to a def. This patch adds a wrapper for this common case to reduce the amount of boilerplate callers need to duplicate repeatedly. getOperatorAsDef() returns the record if the DagInit has an operator that is a DefInit. Otherwise, it prints a fatal error. There's only a few pre-existing examples in LLVM at the moment and I've left a few instances of the code this simplifies as they had more specific error messages than the generic one this produces. I'm going to be using this a fair bit in my subsequent patches. Reviewers: bogner, volkan, nhaehnle Reviewed By: nhaehnle Subscribers: nhaehnle, hiraditya, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei, lenary, s.egerton, pzheng, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D68424 llvm-svn: 374101
1 parent 28fcc03 commit 4b7cabf

File tree

4 files changed

+11
-13
lines changed

4 files changed

+11
-13
lines changed
 

‎llvm/include/llvm/TableGen/Record.h

+1
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,7 @@ class DagInit final : public TypedInit, public FoldingSetNode,
13301330
void Profile(FoldingSetNodeID &ID) const;
13311331

13321332
Init *getOperator() const { return Val; }
1333+
Record *getOperatorAsDef(ArrayRef<SMLoc> Loc) const;
13331334

13341335
StringInit *getName() const { return ValName; }
13351336

‎llvm/lib/TableGen/Record.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,13 @@ void DagInit::Profile(FoldingSetNodeID &ID) const {
19301930
ProfileDagInit(ID, Val, ValName, makeArrayRef(getTrailingObjects<Init *>(), NumArgs), makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames));
19311931
}
19321932

1933+
Record *DagInit::getOperatorAsDef(ArrayRef<SMLoc> Loc) const {
1934+
if (DefInit *DefI = dyn_cast<DefInit>(Val))
1935+
return DefI->getDef();
1936+
PrintFatalError(Loc, "Expected record as operator");
1937+
return nullptr;
1938+
}
1939+
19331940
Init *DagInit::resolveReferences(Resolver &R) const {
19341941
SmallVector<Init*, 8> NewArgs;
19351942
NewArgs.reserve(arg_size());

‎llvm/utils/TableGen/AsmWriterEmitter.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -784,8 +784,7 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
784784
continue; // Aliases with priority 0 are never emitted.
785785

786786
const DagInit *DI = R->getValueAsDag("ResultInst");
787-
const DefInit *Op = cast<DefInit>(DI->getOperator());
788-
AliasMap[getQualifiedName(Op->getDef())].insert(
787+
AliasMap[getQualifiedName(DI->getOperatorAsDef(R->getLoc()))].insert(
789788
std::make_pair(CodeGenInstAlias(R, Target), Priority));
790789
}
791790

‎llvm/utils/TableGen/RISCVCompressInstEmitter.cpp

+2-11
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,8 @@ void RISCVCompressInstEmitter::evaluateCompressPat(Record *Rec) {
411411
assert(SourceDag && "Missing 'Input' in compress pattern!");
412412
LLVM_DEBUG(dbgs() << "Input: " << *SourceDag << "\n");
413413

414-
DefInit *OpDef = dyn_cast<DefInit>(SourceDag->getOperator());
415-
if (!OpDef)
416-
PrintFatalError(Rec->getLoc(),
417-
Rec->getName() + " has unexpected operator type!");
418414
// Checking we are transforming from compressed to uncompressed instructions.
419-
Record *Operator = OpDef->getDef();
415+
Record *Operator = SourceDag->getOperatorAsDef(Rec->getLoc());
420416
if (!Operator->isSubClassOf("RVInst"))
421417
PrintFatalError(Rec->getLoc(), "Input instruction '" + Operator->getName() +
422418
"' is not a 32 bit wide instruction!");
@@ -428,12 +424,7 @@ void RISCVCompressInstEmitter::evaluateCompressPat(Record *Rec) {
428424
assert(DestDag && "Missing 'Output' in compress pattern!");
429425
LLVM_DEBUG(dbgs() << "Output: " << *DestDag << "\n");
430426

431-
DefInit *DestOpDef = dyn_cast<DefInit>(DestDag->getOperator());
432-
if (!DestOpDef)
433-
PrintFatalError(Rec->getLoc(),
434-
Rec->getName() + " has unexpected operator type!");
435-
436-
Record *DestOperator = DestOpDef->getDef();
427+
Record *DestOperator = DestDag->getOperatorAsDef(Rec->getLoc());
437428
if (!DestOperator->isSubClassOf("RVInst16"))
438429
PrintFatalError(Rec->getLoc(), "Output instruction '" +
439430
DestOperator->getName() +

0 commit comments

Comments
 (0)
Please sign in to comment.