Skip to content

Commit ca6ac74

Browse files
authored
[NFC][TableGen] Use BitsInit::convertInitializerToInt in a few places (#156973)
- Replace manual code to convert a `BitsInit` to a uint64_t by using `convertInitializerToInt` where applicable. - Add `BitsInit::convertKnownBitsToInt` to handle existing patterns in DFAEmitter.cpp and RegisterInfoEmitter.cpp. - Consolidate 3 copies of the same function in X86 emitters into a single function.
1 parent 68f8e6e commit ca6ac74

File tree

9 files changed

+31
-71
lines changed

9 files changed

+31
-71
lines changed

llvm/include/llvm/TableGen/Record.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,9 @@ class BitsInit final : public TypedInit,
616616
convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
617617
std::optional<int64_t> convertInitializerToInt() const;
618618

619+
// Returns the set of known bits as a 64-bit integer.
620+
uint64_t convertKnownBitsToInt() const;
621+
619622
bool isComplete() const override;
620623
bool allInComplete() const;
621624
bool isConcrete() const override;

llvm/lib/TableGen/Record.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,14 @@ std::optional<int64_t> BitsInit::convertInitializerToInt() const {
525525
return Result;
526526
}
527527

528+
uint64_t BitsInit::convertKnownBitsToInt() const {
529+
uint64_t Result = 0;
530+
for (auto [Idx, InitV] : enumerate(getBits()))
531+
if (auto *Bit = dyn_cast<BitInit>(InitV))
532+
Result |= static_cast<int64_t>(Bit->getValue()) << Idx;
533+
return Result;
534+
}
535+
528536
const Init *
529537
BitsInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
530538
SmallVector<const Init *, 16> NewBits(Bits.size());

llvm/utils/TableGen/DFAEmitter.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,16 +303,9 @@ StringRef Automaton::getActionSymbolType(StringRef A) {
303303

304304
Transition::Transition(const Record *R, Automaton *Parent) {
305305
const BitsInit *NewStateInit = R->getValueAsBitsInit("NewState");
306-
NewState = 0;
307306
assert(NewStateInit->getNumBits() <= sizeof(uint64_t) * 8 &&
308307
"State cannot be represented in 64 bits!");
309-
for (unsigned I = 0; I < NewStateInit->getNumBits(); ++I) {
310-
if (auto *Bit = dyn_cast<BitInit>(NewStateInit->getBit(I))) {
311-
if (Bit->getValue())
312-
NewState |= 1ULL << I;
313-
}
314-
}
315-
308+
NewState = NewStateInit->convertKnownBitsToInt();
316309
for (StringRef A : Parent->getActionSymbolFields()) {
317310
const RecordVal *SymbolV = R->getValue(A);
318311
if (const auto *Ty = dyn_cast<RecordRecTy>(SymbolV->getType())) {

llvm/utils/TableGen/InstrInfoEmitter.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,16 +1273,12 @@ void InstrInfoEmitter::emitRecord(
12731273
const BitsInit *TSF = Inst.TheDef->getValueAsBitsInit("TSFlags");
12741274
if (!TSF)
12751275
PrintFatalError(Inst.TheDef->getLoc(), "no TSFlags?");
1276-
uint64_t Value = 0;
1277-
for (unsigned i = 0, e = TSF->getNumBits(); i != e; ++i) {
1278-
if (const auto *Bit = dyn_cast<BitInit>(TSF->getBit(i)))
1279-
Value |= uint64_t(Bit->getValue()) << i;
1280-
else
1281-
PrintFatalError(Inst.TheDef->getLoc(),
1282-
"Invalid TSFlags bit in " + Inst.getName());
1283-
}
1276+
std::optional<uint64_t> Value = TSF->convertInitializerToInt();
1277+
if (!Value)
1278+
PrintFatalError(Inst.TheDef, "Invalid TSFlags bit in " + Inst.getName());
1279+
12841280
OS << ", 0x";
1285-
OS.write_hex(Value);
1281+
OS.write_hex(*Value);
12861282
OS << "ULL";
12871283

12881284
OS << " }, // " << Inst.getName() << '\n';

llvm/utils/TableGen/RegisterInfoEmitter.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,11 +1107,7 @@ void RegisterInfoEmitter::runMCDesc(raw_ostream &OS) {
11071107
for (const auto &RE : Regs) {
11081108
const Record *Reg = RE.TheDef;
11091109
const BitsInit *BI = Reg->getValueAsBitsInit("HWEncoding");
1110-
uint64_t Value = 0;
1111-
for (unsigned b = 0, be = BI->getNumBits(); b != be; ++b) {
1112-
if (const BitInit *B = dyn_cast<BitInit>(BI->getBit(b)))
1113-
Value |= (uint64_t)B->getValue() << b;
1114-
}
1110+
uint64_t Value = BI->convertKnownBitsToInt();
11151111
OS << " " << Value << ",\n";
11161112
}
11171113
OS << "};\n"; // End of HW encoding table

llvm/utils/TableGen/X86FoldTablesEmitter.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,6 @@ static bool hasPtrTailcallRegClass(const CodeGenInstruction *Inst) {
245245
});
246246
}
247247

248-
static uint8_t byteFromBitsInit(const BitsInit *B) {
249-
unsigned N = B->getNumBits();
250-
assert(N <= 8 && "Field is too large for uint8_t!");
251-
252-
uint8_t Value = 0;
253-
for (unsigned I = 0; I != N; ++I) {
254-
const BitInit *Bit = cast<BitInit>(B->getBit(I));
255-
Value |= Bit->getValue() << I;
256-
}
257-
return Value;
258-
}
259-
260248
static bool mayFoldFromForm(uint8_t Form) {
261249
switch (Form) {
262250
default:

llvm/utils/TableGen/X86InstrMappingEmitter.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,6 @@ void X86InstrMappingEmitter::printTable(ArrayRef<Entry> Table, StringRef Name,
106106
printMacroEnd(Macro, OS);
107107
}
108108

109-
static uint8_t byteFromBitsInit(const BitsInit *B) {
110-
unsigned N = B->getNumBits();
111-
assert(N <= 8 && "Field is too large for uint8_t!");
112-
113-
uint8_t Value = 0;
114-
for (unsigned I = 0; I != N; ++I) {
115-
const BitInit *Bit = cast<BitInit>(B->getBit(I));
116-
Value |= Bit->getValue() << I;
117-
}
118-
return Value;
119-
}
120-
121109
class IsMatch {
122110
const CodeGenInstruction *OldInst;
123111

llvm/utils/TableGen/X86RecognizableInstr.cpp

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,6 @@ unsigned X86Disassembler::getMemOperandSize(const Record *MemRec) {
7272
llvm_unreachable("Memory operand's size not known!");
7373
}
7474

75-
/// byteFromBitsInit - Extracts a value at most 8 bits in width from a BitsInit.
76-
/// Useful for switch statements and the like.
77-
///
78-
/// @param init - A reference to the BitsInit to be decoded.
79-
/// @return - The field, with the first bit in the BitsInit as the lowest
80-
/// order bit.
81-
static uint8_t byteFromBitsInit(const BitsInit &init) {
82-
int width = init.getNumBits();
83-
84-
assert(width <= 8 && "Field is too large for uint8_t!");
85-
86-
uint8_t mask = 0x01;
87-
uint8_t ret = 0;
88-
89-
for (int index = 0; index < width; index++) {
90-
if (cast<BitInit>(init.getBit(index))->getValue())
91-
ret |= mask;
92-
93-
mask <<= 1;
94-
}
95-
96-
return ret;
97-
}
98-
9975
/// byteFromRec - Extract a value at most 8 bits in with from a Record given the
10076
/// name of the field.
10177
///
@@ -104,7 +80,7 @@ static uint8_t byteFromBitsInit(const BitsInit &init) {
10480
/// @return - The field, as translated by byteFromBitsInit().
10581
static uint8_t byteFromRec(const Record *rec, StringRef name) {
10682
const BitsInit *bits = rec->getValueAsBitsInit(name);
107-
return byteFromBitsInit(*bits);
83+
return byteFromBitsInit(bits);
10884
}
10985

11086
RecognizableInstrBase::RecognizableInstrBase(const CodeGenInstruction &insn) {

llvm/utils/TableGen/X86RecognizableInstr.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,18 @@ bool isMemoryOperand(const Record *Rec);
383383
bool isImmediateOperand(const Record *Rec);
384384
unsigned getRegOperandSize(const Record *RegRec);
385385
unsigned getMemOperandSize(const Record *MemRec);
386+
387+
/// byteFromBitsInit - Extracts a value at most 8 bits in width from a BitsInit.
388+
/// Useful for switch statements and the like.
389+
///
390+
/// @param B - A pointer to the BitsInit to be decoded.
391+
/// @return - The field, with the first bit in the BitsInit as the lowest
392+
/// order bit.
393+
inline uint8_t byteFromBitsInit(const BitsInit *B) {
394+
assert(B->getNumBits() <= 8 && "Field is too large for uint8_t!");
395+
return static_cast<uint8_t>(*B->convertInitializerToInt());
396+
}
397+
386398
} // namespace X86Disassembler
387399
} // namespace llvm
388400
#endif

0 commit comments

Comments
 (0)