Skip to content

Commit 07a6323

Browse files
authored
[TableGen][DecoderEmitter] Turn EncodingAndInst into a class (NFC) (llvm#154230)
The class will get more methods in follow-up patches.
1 parent 227e88b commit 07a6323

File tree

1 file changed

+52
-35
lines changed

1 file changed

+52
-35
lines changed

llvm/utils/TableGen/DecoderEmitter.cpp

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,38 @@ struct OperandInfo {
129129
const_iterator end() const { return Fields.end(); }
130130
};
131131

132+
/// Represents a parsed InstructionEncoding record or a record derived from it.
133+
class InstructionEncoding {
134+
/// The Record this encoding originates from.
135+
const Record *EncodingDef;
136+
137+
/// The instruction this encoding is for.
138+
const CodeGenInstruction *Inst;
139+
140+
/// The name of this encoding (for debugging purposes).
141+
std::string Name;
142+
143+
public:
144+
InstructionEncoding(const Record *EncodingDef, const CodeGenInstruction *Inst)
145+
: EncodingDef(EncodingDef), Inst(Inst) {
146+
const Record *InstDef = Inst->TheDef;
147+
148+
// Give this encoding a name.
149+
if (EncodingDef != InstDef)
150+
Name = (EncodingDef->getName() + Twine(':')).str();
151+
Name.append(InstDef->getName());
152+
}
153+
154+
/// Returns the Record this encoding originates from.
155+
const Record *getRecord() const { return EncodingDef; }
156+
157+
/// Returns the instruction this encoding is for.
158+
const CodeGenInstruction *getInstruction() const { return Inst; }
159+
160+
/// Returns the name of this encoding, for debugging purposes.
161+
StringRef getName() const { return Name; }
162+
};
163+
132164
typedef std::vector<uint32_t> FixupList;
133165
typedef std::vector<FixupList> FixupScopeList;
134166
typedef SmallSetVector<CachedHashString, 16> PredicateSet;
@@ -205,14 +237,6 @@ struct DecoderTableInfo {
205237
}
206238
};
207239

208-
struct EncodingAndInst {
209-
const Record *EncodingDef;
210-
const CodeGenInstruction *Inst;
211-
212-
EncodingAndInst(const Record *EncodingDef, const CodeGenInstruction *Inst)
213-
: EncodingDef(EncodingDef), Inst(Inst) {}
214-
};
215-
216240
using NamespacesHwModesMap = std::map<std::string, std::set<unsigned>>;
217241

218242
class DecoderEmitter {
@@ -221,7 +245,7 @@ class DecoderEmitter {
221245
const CodeGenHwModes &CGH;
222246

223247
/// All parsed encodings.
224-
std::vector<EncodingAndInst> Encodings;
248+
std::vector<InstructionEncoding> Encodings;
225249

226250
/// Encodings IDs for each HwMode. An ID is an index into Encodings.
227251
SmallDenseMap<unsigned, std::vector<unsigned>> EncodingIDsByHwMode;
@@ -316,13 +340,6 @@ struct BitValue {
316340

317341
} // end anonymous namespace
318342

319-
static raw_ostream &operator<<(raw_ostream &OS, const EncodingAndInst &Value) {
320-
if (Value.EncodingDef != Value.Inst->TheDef)
321-
OS << Value.EncodingDef->getName() << ":";
322-
OS << Value.Inst->TheDef->getName();
323-
return OS;
324-
}
325-
326343
// Prints the bit value for each position.
327344
static void dumpBits(raw_ostream &OS, const BitsInit &Bits, unsigned BitWidth) {
328345
for (const Init *Bit : reverse(Bits.getBits().take_front(BitWidth)))
@@ -500,7 +517,7 @@ class FilterChooser {
500517
friend class Filter;
501518

502519
// Vector of encodings to choose our filter.
503-
ArrayRef<EncodingAndInst> Encodings;
520+
ArrayRef<InstructionEncoding> Encodings;
504521

505522
// Vector of encoding IDs for this filter chooser to work on.
506523
ArrayRef<unsigned> EncodingIDs;
@@ -531,7 +548,7 @@ class FilterChooser {
531548
};
532549

533550
public:
534-
FilterChooser(ArrayRef<EncodingAndInst> Encodings,
551+
FilterChooser(ArrayRef<InstructionEncoding> Encodings,
535552
ArrayRef<unsigned> EncodingIDs,
536553
const std::map<unsigned, std::vector<OperandInfo>> &Ops,
537554
unsigned BW, const DecoderEmitter *E)
@@ -541,7 +558,7 @@ class FilterChooser {
541558
doFilter();
542559
}
543560

544-
FilterChooser(ArrayRef<EncodingAndInst> Encodings,
561+
FilterChooser(ArrayRef<InstructionEncoding> Encodings,
545562
ArrayRef<unsigned> EncodingIDs,
546563
const std::map<unsigned, std::vector<OperandInfo>> &Ops,
547564
const std::vector<BitValue> &ParentFilterBitValues,
@@ -560,7 +577,7 @@ class FilterChooser {
560577
protected:
561578
// Populates the insn given the uid.
562579
insn_t insnWithID(unsigned EncodingID) const {
563-
const Record *EncodingDef = Encodings[EncodingID].EncodingDef;
580+
const Record *EncodingDef = Encodings[EncodingID].getRecord();
564581
const BitsInit &Bits = getBitsField(*EncodingDef, "Inst");
565582
insn_t Insn(std::max(BitWidth, Bits.getNumBits()), BitValue::BIT_UNSET);
566583
// We may have a SoftFail bitmask, which specifies a mask where an encoding
@@ -827,7 +844,7 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
827844
DenseMap<unsigned, unsigned> OpcodeToEncodingID;
828845
OpcodeToEncodingID.reserve(EncodingIDs.size());
829846
for (unsigned EncodingID : EncodingIDs) {
830-
const Record *InstDef = Encodings[EncodingID].Inst->TheDef;
847+
const Record *InstDef = Encodings[EncodingID].getInstruction()->TheDef;
831848
OpcodeToEncodingID[Target.getInstrIntValue(InstDef)] = EncodingID;
832849
}
833850

@@ -978,15 +995,15 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
978995
auto EncodingID = EncI->second;
979996

980997
if (!IsTry) {
981-
OS << "// Opcode: " << Encodings[EncodingID]
998+
OS << "// Opcode: " << Encodings[EncodingID].getName()
982999
<< ", DecodeIdx: " << DecodeIdx << '\n';
9831000
break;
9841001
}
9851002

9861003
// Fallthrough for OPC_TryDecode.
9871004
if (!IsFail) {
9881005
uint32_t NumToSkip = emitNumToSkip(I, OS);
989-
OS << "// Opcode: " << Encodings[EncodingID]
1006+
OS << "// Opcode: " << Encodings[EncodingID].getName()
9901007
<< ", DecodeIdx: " << DecodeIdx;
9911008
emitNumToSkipComment(NumToSkip, /*InComment=*/true);
9921009
}
@@ -1295,7 +1312,7 @@ bool FilterChooser::emitPredicateMatchAux(const Init &Val, bool ParenIfBinOp,
12951312
bool FilterChooser::emitPredicateMatch(raw_ostream &OS,
12961313
unsigned EncodingID) const {
12971314
const ListInit *Predicates =
1298-
Encodings[EncodingID].EncodingDef->getValueAsListInit("Predicates");
1315+
Encodings[EncodingID].getRecord()->getValueAsListInit("Predicates");
12991316
bool IsFirstEmission = true;
13001317
for (unsigned i = 0; i < Predicates->size(); ++i) {
13011318
const Record *Pred = Predicates->getElementAsRecord(i);
@@ -1317,7 +1334,7 @@ bool FilterChooser::emitPredicateMatch(raw_ostream &OS,
13171334

13181335
bool FilterChooser::doesOpcodeNeedPredicate(unsigned EncodingID) const {
13191336
const ListInit *Predicates =
1320-
Encodings[EncodingID].EncodingDef->getValueAsListInit("Predicates");
1337+
Encodings[EncodingID].getRecord()->getValueAsListInit("Predicates");
13211338
for (unsigned i = 0; i < Predicates->size(); ++i) {
13221339
const Record *Pred = Predicates->getElementAsRecord(i);
13231340
if (!Pred->getValue("AssemblerMatcherPredicate"))
@@ -1374,7 +1391,7 @@ void FilterChooser::emitPredicateTableEntry(DecoderTableInfo &TableInfo,
13741391

13751392
void FilterChooser::emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
13761393
unsigned EncodingID) const {
1377-
const Record *EncodingDef = Encodings[EncodingID].EncodingDef;
1394+
const Record *EncodingDef = Encodings[EncodingID].getRecord();
13781395
const RecordVal *RV = EncodingDef->getValue("SoftFail");
13791396
const BitsInit *SFBits = RV ? dyn_cast<BitsInit>(RV->getValue()) : nullptr;
13801397

@@ -1400,7 +1417,7 @@ void FilterChooser::emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
14001417
} else {
14011418
// The bit is not set; this must be an error!
14021419
errs() << "SoftFail Conflict: bit SoftFail{" << i << "} in "
1403-
<< Encodings[EncodingID] << " is set but Inst{" << i
1420+
<< Encodings[EncodingID].getName() << " is set but Inst{" << i
14041421
<< "} is unset!\n"
14051422
<< " - You can only mark a bit as SoftFail if it is fully defined"
14061423
<< " (1/0 - not '?') in Inst\n";
@@ -1473,7 +1490,7 @@ void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
14731490
: MCD::OPC_TryDecode);
14741491
TableInfo.Table.push_back(DecoderOp);
14751492
NumEncodingsSupported++;
1476-
const Record *InstDef = Encodings[EncodingID].Inst->TheDef;
1493+
const Record *InstDef = Encodings[EncodingID].getInstruction()->TheDef;
14771494
TableInfo.Table.insertULEB128(Emitter->getTarget().getInstrIntValue(InstDef));
14781495
TableInfo.Table.insertULEB128(DIdx);
14791496

@@ -1768,10 +1785,10 @@ void FilterChooser::doFilter() {
17681785

17691786
// Dump encodings.
17701787
for (unsigned EncodingID : EncodingIDs) {
1771-
const EncodingAndInst &Enc = Encodings[EncodingID];
1788+
const InstructionEncoding &Enc = Encodings[EncodingID];
17721789
errs() << Indent;
1773-
dumpBits(errs(), getBitsField(*Enc.EncodingDef, "Inst"), BitWidth);
1774-
errs() << " " << Enc << '\n';
1790+
dumpBits(errs(), getBitsField(*Enc.getRecord(), "Inst"), BitWidth);
1791+
errs() << " " << Enc.getName() << '\n';
17751792
}
17761793
PrintFatalError("Decoding conflict encountered");
17771794
}
@@ -2437,7 +2454,7 @@ void DecoderEmitter::handleHwModesUnrelatedEncodings(
24372454
break;
24382455
}
24392456
case SUPPRESSION_LEVEL1: {
2440-
const Record *InstDef = Encodings[EncodingID].Inst->TheDef;
2457+
const Record *InstDef = Encodings[EncodingID].getInstruction()->TheDef;
24412458
std::string DecoderNamespace =
24422459
InstDef->getValueAsString("DecoderNamespace").str();
24432460
auto It = NamespacesWithHwModes.find(DecoderNamespace);
@@ -2540,9 +2557,9 @@ namespace {
25402557

25412558
for (const auto &[HwModeID, EncodingIDs] : EncodingIDsByHwMode) {
25422559
for (unsigned EncodingID : EncodingIDs) {
2543-
const EncodingAndInst &Encoding = Encodings[EncodingID];
2544-
const Record *EncodingDef = Encoding.EncodingDef;
2545-
const CodeGenInstruction *Inst = Encoding.Inst;
2560+
const InstructionEncoding &Encoding = Encodings[EncodingID];
2561+
const Record *EncodingDef = Encoding.getRecord();
2562+
const CodeGenInstruction *Inst = Encoding.getInstruction();
25462563
const Record *Def = Inst->TheDef;
25472564
unsigned Size = EncodingDef->getValueAsInt("Size");
25482565
if (Def->getValueAsString("Namespace") == "TargetOpcode" ||

0 commit comments

Comments
 (0)