Skip to content

Commit 749372b

Browse files
authored
[NFC][TableGen] Code cleanup in CodeGenTarget.cpp (llvm#125569)
- Use StringRef::str() instead of std::string(StringRef). - Use const pointers for `Candidates` in getSuperRegForSubReg(). - Make `AsmParserCat` and `AsmWriterCat` static. - Use enumerate() in `ComputeInstrsByEnum` to assign inst enums. - Use range-based for loops.
1 parent fbe470c commit 749372b

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

llvm/utils/TableGen/Common/CodeGenTarget.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
#include <tuple>
2929
using namespace llvm;
3030

31-
cl::OptionCategory AsmParserCat("Options for -gen-asm-parser");
32-
cl::OptionCategory AsmWriterCat("Options for -gen-asm-writer");
31+
static cl::OptionCategory AsmParserCat("Options for -gen-asm-parser");
32+
static cl::OptionCategory AsmWriterCat("Options for -gen-asm-writer");
3333

3434
static cl::opt<unsigned>
3535
AsmParserNum("asmparsernum", cl::init(0),
@@ -64,9 +64,9 @@ StringRef llvm::getEnumName(MVT::SimpleValueType T) {
6464
std::string llvm::getQualifiedName(const Record *R) {
6565
std::string Namespace;
6666
if (R->getValue("Namespace"))
67-
Namespace = std::string(R->getValueAsString("Namespace"));
67+
Namespace = R->getValueAsString("Namespace").str();
6868
if (Namespace.empty())
69-
return std::string(R->getName());
69+
return R->getName().str();
7070
return Namespace + "::" + R->getName().str();
7171
}
7272

@@ -166,14 +166,15 @@ CodeGenRegBank &CodeGenTarget::getRegBank() const {
166166
const CodeGenRegisterClass *CodeGenTarget::getSuperRegForSubReg(
167167
const ValueTypeByHwMode &ValueTy, CodeGenRegBank &RegBank,
168168
const CodeGenSubRegIndex *SubIdx, bool MustBeAllocatable) const {
169-
std::vector<CodeGenRegisterClass *> Candidates;
169+
std::vector<const CodeGenRegisterClass *> Candidates;
170170
auto &RegClasses = RegBank.getRegClasses();
171171

172172
// Try to find a register class which supports ValueTy, and also contains
173173
// SubIdx.
174-
for (CodeGenRegisterClass &RC : RegClasses) {
174+
for (const CodeGenRegisterClass &RC : RegClasses) {
175175
// Is there a subclass of this class which contains this subregister index?
176-
CodeGenRegisterClass *SubClassWithSubReg = RC.getSubClassWithSubReg(SubIdx);
176+
const CodeGenRegisterClass *SubClassWithSubReg =
177+
RC.getSubClassWithSubReg(SubIdx);
177178
if (!SubClassWithSubReg)
178179
continue;
179180

@@ -268,32 +269,32 @@ void CodeGenTarget::ReadInstructions() const {
268269
}
269270

270271
static const CodeGenInstruction *GetInstByName(
271-
const char *Name,
272+
StringRef Name,
272273
const DenseMap<const Record *, std::unique_ptr<CodeGenInstruction>> &Insts,
273274
const RecordKeeper &Records) {
274275
const Record *Rec = Records.getDef(Name);
275276

276277
const auto I = Insts.find(Rec);
277278
if (!Rec || I == Insts.end())
278-
PrintFatalError(Twine("Could not find '") + Name + "' instruction!");
279+
PrintFatalError("Could not find '" + Name + "' instruction!");
279280
return I->second.get();
280281
}
281282

282283
static const char *FixedInstrs[] = {
283284
#define HANDLE_TARGET_OPCODE(OPC) #OPC,
284285
#include "llvm/Support/TargetOpcodes.def"
285-
nullptr};
286+
};
286287

287288
unsigned CodeGenTarget::getNumFixedInstructions() {
288-
return std::size(FixedInstrs) - 1;
289+
return std::size(FixedInstrs);
289290
}
290291

291292
/// Return all of the instructions defined by the target, ordered by
292293
/// their enum value.
293294
void CodeGenTarget::ComputeInstrsByEnum() const {
294295
const auto &Insts = getInstructions();
295-
for (const char *const *p = FixedInstrs; *p; ++p) {
296-
const CodeGenInstruction *Instr = GetInstByName(*p, Insts, Records);
296+
for (const char *Name : FixedInstrs) {
297+
const CodeGenInstruction *Instr = GetInstByName(Name, Insts, Records);
297298
assert(Instr && "Missing target independent instruction");
298299
assert(Instr->Namespace == "TargetOpcode" && "Bad namespace");
299300
InstrsByEnum.push_back(Instr);
@@ -324,9 +325,8 @@ void CodeGenTarget::ComputeInstrsByEnum() const {
324325
});
325326

326327
// Assign an enum value to each instruction according to the sorted order.
327-
unsigned Num = 0;
328-
for (const CodeGenInstruction *Inst : InstrsByEnum)
329-
Inst->EnumVal = Num++;
328+
for (const auto &[Idx, Inst] : enumerate(InstrsByEnum))
329+
Inst->EnumVal = Idx;
330330
}
331331

332332
/// isLittleEndianEncoding - Return whether this target encodes its instruction

0 commit comments

Comments
 (0)