Skip to content

Commit 4f680ce

Browse files
committed
[LLVM][TableGen] Minor cleanup in CodeGenInstruction
- Use StringRef instead of std::string for `AsmString`. - Change `hadOperandNamed` to return index as std::optional and rename it to `findOperandNamed`. - Change `SubOperandAlias` to return std::optional and rename it to `findSubOperandAlias`.
1 parent 711f6a8 commit 4f680ce

File tree

4 files changed

+34
-43
lines changed

4 files changed

+34
-43
lines changed

llvm/utils/TableGen/AsmMatcherEmitter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,11 +1712,11 @@ void AsmMatcherInfo::buildInstructionOperandReference(MatchableInfo *II,
17121712
MatchableInfo::AsmOperand *Op = &II->AsmOperands[AsmOpIdx];
17131713

17141714
// Map this token to an operand.
1715-
unsigned Idx;
1716-
if (!Operands.hasOperandNamed(OperandName, Idx))
1715+
std::optional<unsigned> MayBeIdx = Operands.findOperandNamed(OperandName);
1716+
if (!MayBeIdx)
17171717
PrintFatalError(II->TheDef->getLoc(),
17181718
"error: unable to find operand: '" + OperandName + "'");
1719-
1719+
unsigned Idx = *MayBeIdx;
17201720
// If the instruction operand has multiple suboperands, but the parser
17211721
// match class for the asm operand is still the default "ImmAsmOperand",
17221722
// then handle each suboperand separately.

llvm/utils/TableGen/CodeEmitterGen.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,11 @@ bool CodeEmitterGen::addCodeToMergeInOperand(const Record *R,
123123
// operand number. Non-matching operands are assumed to be in
124124
// order.
125125
unsigned OpIdx;
126-
std::pair<unsigned, unsigned> SubOp;
127-
if (CGI.Operands.hasSubOperandAlias(VarName, SubOp)) {
128-
OpIdx = CGI.Operands[SubOp.first].MIOperandNo + SubOp.second;
129-
} else if (CGI.Operands.hasOperandNamed(VarName, OpIdx)) {
126+
if (auto SubOp = CGI.Operands.findSubOperandAlias(VarName)) {
127+
OpIdx = CGI.Operands[SubOp->first].MIOperandNo + SubOp->second;
128+
} else if (auto MayBeOpIdx = CGI.Operands.findOperandNamed(VarName)) {
130129
// Get the machine operand number for the indicated operand.
131-
OpIdx = CGI.Operands[OpIdx].MIOperandNo;
130+
OpIdx = CGI.Operands[*MayBeOpIdx].MIOperandNo;
132131
} else {
133132
PrintError(R, Twine("No operand named ") + VarName + " in record " +
134133
R->getName());

llvm/utils/TableGen/Common/CodeGenInstruction.cpp

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ CGIOperandList::CGIOperandList(const Record *R) : TheDef(R) {
183183
// If we have no explicit sub-op dag, but have an top-level encoder
184184
// method, the single encoder will multiple sub-ops, itself.
185185
OpInfo.EncoderMethodNames[0] = EncoderMethod;
186-
for (unsigned j = 1; j < NumOps; ++j)
187-
OpInfo.DoNotEncode[j] = true;
186+
OpInfo.DoNotEncode.set();
187+
OpInfo.DoNotEncode[0] = false;
188188
}
189189

190190
MIOperandNo += NumOps;
@@ -199,36 +199,31 @@ CGIOperandList::CGIOperandList(const Record *R) : TheDef(R) {
199199
/// specified name, abort.
200200
///
201201
unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
202-
unsigned OpIdx;
203-
if (hasOperandNamed(Name, OpIdx))
204-
return OpIdx;
202+
std::optional<unsigned> OpIdx = findOperandNamed(Name);
203+
if (OpIdx)
204+
return *OpIdx;
205205
PrintFatalError(TheDef->getLoc(), "'" + TheDef->getName() +
206206
"' does not have an operand named '$" +
207207
Name + "'!");
208208
}
209209

210-
/// hasOperandNamed - Query whether the instruction has an operand of the
211-
/// given name. If so, return true and set OpIdx to the index of the
212-
/// operand. Otherwise, return false.
213-
bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
210+
/// findOperandNamed - Query whether the instruction has an operand of the
211+
/// given name. If so, the index of the operand. Otherwise, return std::nullopt.
212+
std::optional<unsigned> CGIOperandList::findOperandNamed(StringRef Name) const {
214213
assert(!Name.empty() && "Cannot search for operand with no name!");
215-
for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
216-
if (OperandList[i].Name == Name) {
217-
OpIdx = i;
218-
return true;
219-
}
220-
return false;
214+
for (const auto &[Index, Opnd] : enumerate(OperandList))
215+
if (Opnd.Name == Name)
216+
return Index;
217+
return std::nullopt;
221218
}
222219

223-
bool CGIOperandList::hasSubOperandAlias(
224-
StringRef Name, std::pair<unsigned, unsigned> &SubOp) const {
220+
std::optional<std::pair<unsigned, unsigned>>
221+
CGIOperandList::findSubOperandAlias(StringRef Name) const {
225222
assert(!Name.empty() && "Cannot search for operand with no name!");
226223
auto SubOpIter = SubOpAliases.find(Name);
227-
if (SubOpIter != SubOpAliases.end()) {
228-
SubOp = SubOpIter->second;
229-
return true;
230-
}
231-
return false;
224+
if (SubOpIter != SubOpAliases.end())
225+
return SubOpIter->second;
226+
return std::nullopt;
232227
}
233228

234229
std::pair<unsigned, unsigned>
@@ -251,9 +246,7 @@ CGIOperandList::ParseOperandName(StringRef Op, bool AllowWholeOp) {
251246
OpName = OpName.substr(0, DotIdx);
252247
}
253248

254-
unsigned OpIdx;
255-
256-
if (std::pair<unsigned, unsigned> SubOp; hasSubOperandAlias(OpName, SubOp)) {
249+
if (auto SubOp = findSubOperandAlias(OpName)) {
257250
// Found a name for a piece of an operand, just return it directly.
258251
if (!SubOpName.empty()) {
259252
PrintFatalError(
@@ -262,10 +255,10 @@ CGIOperandList::ParseOperandName(StringRef Op, bool AllowWholeOp) {
262255
": Cannot use dotted suboperand name within suboperand '" +
263256
OpName + "'");
264257
}
265-
return SubOp;
258+
return *SubOp;
266259
}
267260

268-
OpIdx = getOperandNamed(OpName);
261+
unsigned OpIdx = getOperandNamed(OpName);
269262

270263
if (SubOpName.empty()) { // If no suboperand name was specified:
271264
// If one was needed, throw.

llvm/utils/TableGen/Common/CodeGenInstruction.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,9 @@ class CGIOperandList {
127127
/// getTiedOperand - If this operand is tied to another one, return the
128128
/// other operand number. Otherwise, return -1.
129129
int getTiedRegister() const {
130-
for (const CGIOperandList::ConstraintInfo &CI : Constraints) {
130+
for (const CGIOperandList::ConstraintInfo &CI : Constraints)
131131
if (CI.isTied())
132132
return CI.getTiedOperand();
133-
}
134133
return -1;
135134
}
136135
};
@@ -176,13 +175,13 @@ class CGIOperandList {
176175
/// specified name, abort.
177176
unsigned getOperandNamed(StringRef Name) const;
178177

179-
/// hasOperandNamed - Query whether the instruction has an operand of the
180-
/// given name. If so, return true and set OpIdx to the index of the
181-
/// operand. Otherwise, return false.
182-
bool hasOperandNamed(StringRef Name, unsigned &OpIdx) const;
178+
/// findOperandNamed - Query whether the instruction has an operand of the
179+
/// given name. If so, the index of the operand. Otherwise, return
180+
/// std::nullopt.
181+
std::optional<unsigned> findOperandNamed(StringRef Name) const;
183182

184-
bool hasSubOperandAlias(StringRef Name,
185-
std::pair<unsigned, unsigned> &SubOp) const;
183+
std::optional<std::pair<unsigned, unsigned>>
184+
findSubOperandAlias(StringRef Name) const;
186185

187186
/// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
188187
/// where $foo is a whole operand and $foo.bar refers to a suboperand.

0 commit comments

Comments
 (0)