Skip to content

Commit 0532ac4

Browse files
authored
[NFC][AsmMatcherEmitter] Misc code cleanup (#157012)
1 parent e5e5736 commit 0532ac4

File tree

1 file changed

+34
-59
lines changed

1 file changed

+34
-59
lines changed

llvm/utils/TableGen/AsmMatcherEmitter.cpp

Lines changed: 34 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -842,43 +842,35 @@ LLVM_DUMP_METHOD void MatchableInfo::dump() const {
842842

843843
static std::pair<StringRef, StringRef>
844844
parseTwoOperandConstraint(StringRef S, ArrayRef<SMLoc> Loc) {
845+
// Trim whitespace and the leading '$' on the operand names.
846+
auto TrimWSDollar = [Loc](StringRef OpName) {
847+
OpName = OpName.trim(" \t");
848+
if (!OpName.consume_front("$"))
849+
PrintFatalError(Loc, "expected '$' prefix on asm operand name");
850+
return OpName;
851+
};
852+
845853
// Split via the '='.
846-
std::pair<StringRef, StringRef> Ops = S.split('=');
847-
if (Ops.second == "")
854+
auto [Src, Dst] = S.split('=');
855+
if (Dst == "")
848856
PrintFatalError(Loc, "missing '=' in two-operand alias constraint");
849-
// Trim whitespace and the leading '$' on the operand names.
850-
size_t start = Ops.first.find_first_of('$');
851-
if (start == std::string::npos)
852-
PrintFatalError(Loc, "expected '$' prefix on asm operand name");
853-
Ops.first = Ops.first.substr(start + 1);
854-
size_t end = Ops.first.find_last_of(" \t");
855-
Ops.first = Ops.first.slice(0, end);
856-
// Now the second operand.
857-
start = Ops.second.find_first_of('$');
858-
if (start == std::string::npos)
859-
PrintFatalError(Loc, "expected '$' prefix on asm operand name");
860-
Ops.second = Ops.second.substr(start + 1);
861-
end = Ops.second.find_last_of(" \t");
862-
Ops.first = Ops.first.slice(0, end);
863-
return Ops;
857+
return {TrimWSDollar(Src), TrimWSDollar(Dst)};
864858
}
865859

866860
void MatchableInfo::formTwoOperandAlias(StringRef Constraint) {
867861
// Figure out which operands are aliased and mark them as tied.
868-
std::pair<StringRef, StringRef> Ops =
869-
parseTwoOperandConstraint(Constraint, TheDef->getLoc());
862+
auto [Src, Dst] = parseTwoOperandConstraint(Constraint, TheDef->getLoc());
870863

871864
// Find the AsmOperands that refer to the operands we're aliasing.
872-
int SrcAsmOperand = findAsmOperandNamed(Ops.first);
873-
int DstAsmOperand = findAsmOperandNamed(Ops.second);
865+
int SrcAsmOperand = findAsmOperandNamed(Src);
866+
int DstAsmOperand = findAsmOperandNamed(Dst);
874867
if (SrcAsmOperand == -1)
875868
PrintFatalError(TheDef->getLoc(),
876-
"unknown source two-operand alias operand '" + Ops.first +
877-
"'.");
869+
"unknown source two-operand alias operand '" + Src + "'.");
878870
if (DstAsmOperand == -1)
879871
PrintFatalError(TheDef->getLoc(),
880-
"unknown destination two-operand alias operand '" +
881-
Ops.second + "'.");
872+
"unknown destination two-operand alias operand '" + Dst +
873+
"'.");
882874

883875
// Find the ResOperand that refers to the operand we're aliasing away
884876
// and update it to refer to the combined operand instead.
@@ -894,15 +886,9 @@ void MatchableInfo::formTwoOperandAlias(StringRef Constraint) {
894886
// Adjust the ResOperand references to any AsmOperands that followed
895887
// the one we just deleted.
896888
for (ResOperand &Op : ResOperands) {
897-
switch (Op.Kind) {
898-
default:
899-
// Nothing to do for operands that don't reference AsmOperands.
900-
break;
901-
case ResOperand::RenderAsmOperand:
902-
if (Op.AsmOperandNum > (unsigned)SrcAsmOperand)
903-
--Op.AsmOperandNum;
904-
break;
905-
}
889+
if (Op.Kind == ResOperand::RenderAsmOperand &&
890+
Op.AsmOperandNum > (unsigned)SrcAsmOperand)
891+
--Op.AsmOperandNum;
906892
}
907893
}
908894

@@ -925,11 +911,10 @@ static void extractSingletonRegisterForAsmOperand(MatchableInfo::AsmOperand &Op,
925911
return;
926912
}
927913

928-
if (!Tok.starts_with(RegisterPrefix))
914+
if (!Tok.consume_front(RegisterPrefix))
929915
return;
930916

931-
StringRef RegName = Tok.substr(RegisterPrefix.size());
932-
if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(RegName))
917+
if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(Tok))
933918
Op.SingletonReg = Reg->TheDef;
934919

935920
// If there is no register prefix (i.e. "%" in "%eax"), then this may
@@ -1542,10 +1527,9 @@ void AsmMatcherInfo::buildInfo() {
15421527
Variant.AsmVariantNo = AsmVariant->getValueAsInt("Variant");
15431528

15441529
for (const CodeGenInstruction *CGI : Target.getInstructions()) {
1545-
15461530
// If the tblgen -match-prefix option is specified (for tblgen hackers),
15471531
// filter the set of instructions we consider.
1548-
if (!StringRef(CGI->getName()).starts_with(MatchPrefix))
1532+
if (!CGI->getName().starts_with(MatchPrefix))
15491533
continue;
15501534

15511535
// Ignore "codegen only" instructions.
@@ -1578,7 +1562,7 @@ void AsmMatcherInfo::buildInfo() {
15781562
// If the tblgen -match-prefix option is specified (for tblgen hackers),
15791563
// filter the set of instruction aliases we consider, based on the target
15801564
// instruction.
1581-
if (!StringRef(Alias->ResultInst->getName()).starts_with(MatchPrefix))
1565+
if (!Alias->ResultInst->getName().starts_with(MatchPrefix))
15821566
continue;
15831567

15841568
StringRef V = Alias->TheDef->getValueAsString("AsmVariantName");
@@ -2663,11 +2647,8 @@ static void emitMatchRegisterAltName(const CodeGenTarget &Target,
26632647
std::string Namespace =
26642648
Regs.front().TheDef->getValueAsString("Namespace").str();
26652649
for (const CodeGenRegister &Reg : Regs) {
2666-
2667-
auto AltNames = Reg.TheDef->getValueAsListOfStrings("AltNames");
2668-
2669-
for (auto AltName : AltNames) {
2670-
AltName = StringRef(AltName).trim();
2650+
for (StringRef AltName : Reg.TheDef->getValueAsListOfStrings("AltNames")) {
2651+
AltName = AltName.trim();
26712652

26722653
// don't handle empty alternative names
26732654
if (AltName.empty())
@@ -2718,8 +2699,8 @@ static void emitGetSubtargetFeatureName(AsmMatcherInfo &Info, raw_ostream &OS) {
27182699
<< "static const char *getSubtargetFeatureName(uint64_t Val) {\n";
27192700
if (!Info.SubtargetFeatures.empty()) {
27202701
OS << " switch(Val) {\n";
2721-
for (const auto &SF : Info.SubtargetFeatures) {
2722-
const SubtargetFeatureInfo &SFI = SF.second;
2702+
for (const SubtargetFeatureInfo &SFI :
2703+
make_second_range(Info.SubtargetFeatures)) {
27232704
// FIXME: Totally just a placeholder name to get the algorithm working.
27242705
OS << " case " << SFI.getEnumBitName() << ": return \""
27252706
<< SFI.TheDef->getValueAsString("PredicateName") << "\";\n";
@@ -2737,17 +2718,15 @@ static std::string GetAliasRequiredFeatures(const Record *R,
27372718
const AsmMatcherInfo &Info) {
27382719
std::string Result;
27392720

2740-
bool First = true;
2721+
ListSeparator LS(" && ");
27412722
for (const Record *RF : R->getValueAsListOfDefs("Predicates")) {
27422723
const SubtargetFeatureInfo *F = Info.getSubtargetFeature(RF);
27432724
if (!F)
27442725
PrintFatalError(R->getLoc(),
27452726
"Predicate '" + RF->getName() +
27462727
"' is not marked as an AssemblerPredicate!");
2747-
if (!First)
2748-
Result += " && ";
2728+
Result += LS;
27492729
Result += "Features.test(" + F->getEnumBitName() + ')';
2750-
First = false;
27512730
}
27522731

27532732
return Result;
@@ -3638,9 +3617,8 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
36383617
OS << " ErrorInfo = ~0ULL;\n";
36393618
}
36403619

3641-
if (HasOptionalOperands) {
3620+
if (HasOptionalOperands)
36423621
OS << " SmallBitVector OptionalOperandsMask(" << MaxNumOperands << ");\n";
3643-
}
36443622

36453623
// Emit code to search the table.
36463624
OS << " // Find the appropriate table for this asm variant.\n";
@@ -3708,9 +3686,8 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
37083686
// Emit check that the subclasses match.
37093687
if (!ReportMultipleNearMisses)
37103688
OS << " bool OperandsValid = true;\n";
3711-
if (HasOptionalOperands) {
3689+
if (HasOptionalOperands)
37123690
OS << " OptionalOperandsMask.reset(0, " << MaxNumOperands << ");\n";
3713-
}
37143691
OS << " for (unsigned FormalIdx = " << (HasMnemonicFirst ? "0" : "SIndex")
37153692
<< ", ActualIdx = " << (HasMnemonicFirst ? "1" : "SIndex")
37163693
<< "; FormalIdx != " << MaxNumOperands << "; ++FormalIdx) {\n";
@@ -3771,9 +3748,8 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
37713748
OS << " break;\n";
37723749
OS << " }\n";
37733750
OS << " if (isSubclass(Formal, OptionalMatchClass)) {\n";
3774-
if (HasOptionalOperands) {
3751+
if (HasOptionalOperands)
37753752
OS << " OptionalOperandsMask.set(FormalIdx);\n";
3776-
}
37773753
OS << " continue;\n";
37783754
OS << " }\n";
37793755
OS << " OperandsValid = false;\n";
@@ -3812,9 +3788,8 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
38123788
<< " // then try to match next formal operand\n";
38133789
OS << " if (Diag == Match_InvalidOperand "
38143790
<< "&& isSubclass(Formal, OptionalMatchClass)) {\n";
3815-
if (HasOptionalOperands) {
3791+
if (HasOptionalOperands)
38163792
OS << " OptionalOperandsMask.set(FormalIdx);\n";
3817-
}
38183793
OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"ignoring "
38193794
"optional operand\\n\");\n";
38203795
OS << " continue;\n";

0 commit comments

Comments
 (0)