[llvm] [NFC][TableGen] Use `BitsInit::convertInitializerToInt` in a few places (PR #156973)
Rahul Joshi via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 4 15:00:39 PDT 2025
https://github.com/jurahul created https://github.com/llvm/llvm-project/pull/156973
Replace manual code to convert a `BitsInit` to a uint64_t by using `convertInitializerToInt`.
>From b2e62d3c91c5cdfc3bb38d199fdd18b2371d4ec4 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Thu, 4 Sep 2025 14:58:35 -0700
Subject: [PATCH] [NFC][TableGen] Use `BitsInit::convertInitializerToInt` in a
few places
Replace manual code to convert a `BitsInit` to a uint64_t by using
`convertInitializerToInt`.
---
llvm/utils/TableGen/DFAEmitter.cpp | 7 +----
llvm/utils/TableGen/InstrInfoEmitter.cpp | 15 +++++------
llvm/utils/TableGen/RegisterInfoEmitter.cpp | 6 +----
llvm/utils/TableGen/X86FoldTablesEmitter.cpp | 11 ++------
.../utils/TableGen/X86InstrMappingEmitter.cpp | 11 ++------
llvm/utils/TableGen/X86RecognizableInstr.cpp | 27 +++++--------------
6 files changed, 19 insertions(+), 58 deletions(-)
diff --git a/llvm/utils/TableGen/DFAEmitter.cpp b/llvm/utils/TableGen/DFAEmitter.cpp
index 0b90af2ea99fc..58c42ca0bc998 100644
--- a/llvm/utils/TableGen/DFAEmitter.cpp
+++ b/llvm/utils/TableGen/DFAEmitter.cpp
@@ -306,12 +306,7 @@ Transition::Transition(const Record *R, Automaton *Parent) {
NewState = 0;
assert(NewStateInit->getNumBits() <= sizeof(uint64_t) * 8 &&
"State cannot be represented in 64 bits!");
- for (unsigned I = 0; I < NewStateInit->getNumBits(); ++I) {
- if (auto *Bit = dyn_cast<BitInit>(NewStateInit->getBit(I))) {
- if (Bit->getValue())
- NewState |= 1ULL << I;
- }
- }
+ NewState = *NewStateInit->convertInitializerToInt();
for (StringRef A : Parent->getActionSymbolFields()) {
const RecordVal *SymbolV = R->getValue(A);
diff --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp
index 193087ec38d0c..90500fd62c3d6 100644
--- a/llvm/utils/TableGen/InstrInfoEmitter.cpp
+++ b/llvm/utils/TableGen/InstrInfoEmitter.cpp
@@ -1272,16 +1272,13 @@ void InstrInfoEmitter::emitRecord(
const BitsInit *TSF = Inst.TheDef->getValueAsBitsInit("TSFlags");
if (!TSF)
PrintFatalError(Inst.TheDef->getLoc(), "no TSFlags?");
- uint64_t Value = 0;
- for (unsigned i = 0, e = TSF->getNumBits(); i != e; ++i) {
- if (const auto *Bit = dyn_cast<BitInit>(TSF->getBit(i)))
- Value |= uint64_t(Bit->getValue()) << i;
- else
- PrintFatalError(Inst.TheDef->getLoc(),
- "Invalid TSFlags bit in " + Inst.TheDef->getName());
- }
+ std::optional<uint64_t> Value = TSF->convertInitializerToInt();
+ if (!Value)
+ PrintFatalError(Inst.TheDef,
+ "Invalid TSFlags bit in " + Inst.TheDef->getName());
+
OS << ", 0x";
- OS.write_hex(Value);
+ OS.write_hex(*Value);
OS << "ULL";
OS << " }, // " << Inst.TheDef->getName() << '\n';
diff --git a/llvm/utils/TableGen/RegisterInfoEmitter.cpp b/llvm/utils/TableGen/RegisterInfoEmitter.cpp
index 2a311b7ff96b8..1abcc24d6bf85 100644
--- a/llvm/utils/TableGen/RegisterInfoEmitter.cpp
+++ b/llvm/utils/TableGen/RegisterInfoEmitter.cpp
@@ -1107,11 +1107,7 @@ void RegisterInfoEmitter::runMCDesc(raw_ostream &OS) {
for (const auto &RE : Regs) {
const Record *Reg = RE.TheDef;
const BitsInit *BI = Reg->getValueAsBitsInit("HWEncoding");
- uint64_t Value = 0;
- for (unsigned b = 0, be = BI->getNumBits(); b != be; ++b) {
- if (const BitInit *B = dyn_cast<BitInit>(BI->getBit(b)))
- Value |= (uint64_t)B->getValue() << b;
- }
+ uint64_t Value = *BI->convertInitializerToInt();
OS << " " << Value << ",\n";
}
OS << "};\n"; // End of HW encoding table
diff --git a/llvm/utils/TableGen/X86FoldTablesEmitter.cpp b/llvm/utils/TableGen/X86FoldTablesEmitter.cpp
index d63570a88a4cb..515ed27fde4dc 100644
--- a/llvm/utils/TableGen/X86FoldTablesEmitter.cpp
+++ b/llvm/utils/TableGen/X86FoldTablesEmitter.cpp
@@ -246,15 +246,8 @@ static bool hasPtrTailcallRegClass(const CodeGenInstruction *Inst) {
}
static uint8_t byteFromBitsInit(const BitsInit *B) {
- unsigned N = B->getNumBits();
- assert(N <= 8 && "Field is too large for uint8_t!");
-
- uint8_t Value = 0;
- for (unsigned I = 0; I != N; ++I) {
- const BitInit *Bit = cast<BitInit>(B->getBit(I));
- Value |= Bit->getValue() << I;
- }
- return Value;
+ assert(B->getNumBits() <= 8 && "Field is too large for uint8_t!");
+ return static_cast<uint8_t>(*B->convertInitializerToInt());
}
static bool mayFoldFromForm(uint8_t Form) {
diff --git a/llvm/utils/TableGen/X86InstrMappingEmitter.cpp b/llvm/utils/TableGen/X86InstrMappingEmitter.cpp
index 2e8351c951980..afb760539bef0 100644
--- a/llvm/utils/TableGen/X86InstrMappingEmitter.cpp
+++ b/llvm/utils/TableGen/X86InstrMappingEmitter.cpp
@@ -107,15 +107,8 @@ void X86InstrMappingEmitter::printTable(ArrayRef<Entry> Table, StringRef Name,
}
static uint8_t byteFromBitsInit(const BitsInit *B) {
- unsigned N = B->getNumBits();
- assert(N <= 8 && "Field is too large for uint8_t!");
-
- uint8_t Value = 0;
- for (unsigned I = 0; I != N; ++I) {
- const BitInit *Bit = cast<BitInit>(B->getBit(I));
- Value |= Bit->getValue() << I;
- }
- return Value;
+ assert(B->getNumBits() <= 8 && "Field is too large for uint8_t!");
+ return static_cast<uint8_t>(*B->convertInitializerToInt());
}
class IsMatch {
diff --git a/llvm/utils/TableGen/X86RecognizableInstr.cpp b/llvm/utils/TableGen/X86RecognizableInstr.cpp
index a56e07a8939e7..3bb49594083ed 100644
--- a/llvm/utils/TableGen/X86RecognizableInstr.cpp
+++ b/llvm/utils/TableGen/X86RecognizableInstr.cpp
@@ -75,25 +75,12 @@ unsigned X86Disassembler::getMemOperandSize(const Record *MemRec) {
/// byteFromBitsInit - Extracts a value at most 8 bits in width from a BitsInit.
/// Useful for switch statements and the like.
///
-/// @param init - A reference to the BitsInit to be decoded.
-/// @return - The field, with the first bit in the BitsInit as the lowest
-/// order bit.
-static uint8_t byteFromBitsInit(const BitsInit &init) {
- int width = init.getNumBits();
-
- assert(width <= 8 && "Field is too large for uint8_t!");
-
- uint8_t mask = 0x01;
- uint8_t ret = 0;
-
- for (int index = 0; index < width; index++) {
- if (cast<BitInit>(init.getBit(index))->getValue())
- ret |= mask;
-
- mask <<= 1;
- }
-
- return ret;
+/// @param B - A reference to the BitsInit to be decoded.
+/// @return - The field, with the first bit in the BitsInit as the lowest
+/// order bit.
+static uint8_t byteFromBitsInit(const BitsInit *B) {
+ assert(B->getNumBits() <= 8 && "Field is too large for uint8_t!");
+ return static_cast<uint8_t>(*B->convertInitializerToInt());
}
/// byteFromRec - Extract a value at most 8 bits in with from a Record given the
@@ -104,7 +91,7 @@ static uint8_t byteFromBitsInit(const BitsInit &init) {
/// @return - The field, as translated by byteFromBitsInit().
static uint8_t byteFromRec(const Record *rec, StringRef name) {
const BitsInit *bits = rec->getValueAsBitsInit(name);
- return byteFromBitsInit(*bits);
+ return byteFromBitsInit(bits);
}
RecognizableInstrBase::RecognizableInstrBase(const CodeGenInstruction &insn) {
More information about the llvm-commits
mailing list