Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions llvm/include/llvm/TableGen/Record.h
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,9 @@ class BitsInit final : public TypedInit,
convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
std::optional<int64_t> convertInitializerToInt() const;

// Returns the set of known bits as a 64-bit integer.
uint64_t convertKnownBitsToInt() const;

bool isComplete() const override;
bool allInComplete() const;
bool isConcrete() const override;
Expand Down
8 changes: 8 additions & 0 deletions llvm/lib/TableGen/Record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,14 @@ std::optional<int64_t> BitsInit::convertInitializerToInt() const {
return Result;
}

uint64_t BitsInit::convertKnownBitsToInt() const {
uint64_t Result = 0;
for (auto [Idx, InitV] : enumerate(getBits()))
if (auto *Bit = dyn_cast<BitInit>(InitV))
Result |= static_cast<int64_t>(Bit->getValue()) << Idx;
return Result;
}

const Init *
BitsInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
SmallVector<const Init *, 16> NewBits(Bits.size());
Expand Down
9 changes: 1 addition & 8 deletions llvm/utils/TableGen/DFAEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,9 @@ StringRef Automaton::getActionSymbolType(StringRef A) {

Transition::Transition(const Record *R, Automaton *Parent) {
const BitsInit *NewStateInit = R->getValueAsBitsInit("NewState");
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->convertKnownBitsToInt();
for (StringRef A : Parent->getActionSymbolFields()) {
const RecordVal *SymbolV = R->getValue(A);
if (const auto *Ty = dyn_cast<RecordRecTy>(SymbolV->getType())) {
Expand Down
14 changes: 5 additions & 9 deletions llvm/utils/TableGen/InstrInfoEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1273,16 +1273,12 @@ 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.getName());
}
std::optional<uint64_t> Value = TSF->convertInitializerToInt();
if (!Value)
PrintFatalError(Inst.TheDef, "Invalid TSFlags bit in " + Inst.getName());

OS << ", 0x";
OS.write_hex(Value);
OS.write_hex(*Value);
OS << "ULL";

OS << " }, // " << Inst.getName() << '\n';
Expand Down
6 changes: 1 addition & 5 deletions llvm/utils/TableGen/RegisterInfoEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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->convertKnownBitsToInt();
OS << " " << Value << ",\n";
}
OS << "};\n"; // End of HW encoding table
Expand Down
12 changes: 0 additions & 12 deletions llvm/utils/TableGen/X86FoldTablesEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,6 @@ 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;
}

static bool mayFoldFromForm(uint8_t Form) {
switch (Form) {
default:
Expand Down
12 changes: 0 additions & 12 deletions llvm/utils/TableGen/X86InstrMappingEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,6 @@ void X86InstrMappingEmitter::printTable(ArrayRef<Entry> Table, StringRef Name,
printMacroEnd(Macro, OS);
}

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;
}

class IsMatch {
const CodeGenInstruction *OldInst;

Expand Down
26 changes: 1 addition & 25 deletions llvm/utils/TableGen/X86RecognizableInstr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,6 @@ unsigned X86Disassembler::getMemOperandSize(const Record *MemRec) {
llvm_unreachable("Memory operand's size not known!");
}

/// 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;
}

/// byteFromRec - Extract a value at most 8 bits in with from a Record given the
/// name of the field.
///
Expand All @@ -104,7 +80,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) {
Expand Down
12 changes: 12 additions & 0 deletions llvm/utils/TableGen/X86RecognizableInstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,18 @@ bool isMemoryOperand(const Record *Rec);
bool isImmediateOperand(const Record *Rec);
unsigned getRegOperandSize(const Record *RegRec);
unsigned 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 B - A pointer to the BitsInit to be decoded.
/// @return - The field, with the first bit in the BitsInit as the lowest
/// order bit.
inline uint8_t byteFromBitsInit(const BitsInit *B) {
assert(B->getNumBits() <= 8 && "Field is too large for uint8_t!");
return static_cast<uint8_t>(*B->convertInitializerToInt());
}

} // namespace X86Disassembler
} // namespace llvm
#endif