Skip to content

Commit b3fa92f

Browse files
authored
[TableGen][Decoder] Make predicate/decocder generation functions return a string (NFC) (#159089)
These functions will see more uses in a future patch. This also resolves a FIXME.
1 parent 334013b commit b3fa92f

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

llvm/utils/TableGen/DecoderEmitter.cpp

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,55 +1063,51 @@ static void emitBinaryParser(raw_ostream &OS, indent Indent,
10631063
}
10641064
}
10651065

1066-
static void emitDecoder(raw_ostream &OS, indent Indent,
1067-
const InstructionEncoding &Encoding) {
1066+
static std::string getDecoderString(const InstructionEncoding &Encoding) {
1067+
std::string Decoder;
1068+
raw_string_ostream OS(Decoder);
1069+
indent Indent(UseFnTableInDecodeToMCInst ? 2 : 4);
1070+
10681071
// If a custom instruction decoder was specified, use that.
10691072
StringRef DecoderMethod = Encoding.getDecoderMethod();
10701073
if (!DecoderMethod.empty()) {
10711074
OS << Indent << "if (!Check(S, " << DecoderMethod
10721075
<< "(MI, insn, Address, Decoder))) { "
10731076
<< (Encoding.hasCompleteDecoder() ? "" : "DecodeComplete = false; ")
10741077
<< "return MCDisassembler::Fail; }\n";
1075-
return;
1078+
} else {
1079+
for (const OperandInfo &Op : Encoding.getOperands())
1080+
emitBinaryParser(OS, Indent, Encoding, Op);
10761081
}
1077-
1078-
for (const OperandInfo &Op : Encoding.getOperands())
1079-
emitBinaryParser(OS, Indent, Encoding, Op);
1082+
return Decoder;
10801083
}
10811084

10821085
static unsigned getDecoderIndex(const InstructionEncoding &Encoding,
10831086
DecoderTableInfo &TableInfo) {
1084-
// Build up the predicate string.
1085-
SmallString<256> Decoder;
1086-
// FIXME: emitDecoder() function can take a buffer directly rather than
1087-
// a stream.
1088-
raw_svector_ostream S(Decoder);
1089-
indent Indent(UseFnTableInDecodeToMCInst ? 2 : 4);
1090-
emitDecoder(S, Indent, Encoding);
1091-
10921087
// Using the full decoder string as the key value here is a bit
10931088
// heavyweight, but is effective. If the string comparisons become a
10941089
// performance concern, we can implement a mangling of the predicate
10951090
// data easily enough with a map back to the actual string. That's
10961091
// overkill for now, though.
1092+
std::string Decoder = getDecoderString(Encoding);
10971093
TableInfo.insertDecoder(Decoder);
10981094
return TableInfo.getDecoderIndex(Decoder);
10991095
}
11001096

1101-
// Returns true if there was any predicate emitted.
1102-
static bool emitPredicateMatch(raw_ostream &OS,
1103-
const InstructionEncoding &Encoding,
1104-
StringRef TargetName) {
1097+
static std::string getPredicateString(const InstructionEncoding &Encoding,
1098+
StringRef TargetName) {
11051099
std::vector<const Record *> Predicates =
11061100
Encoding.getRecord()->getValueAsListOfDefs("Predicates");
11071101
auto It = llvm::find_if(Predicates, [](const Record *R) {
11081102
return R->getValueAsBit("AssemblerMatcherPredicate");
11091103
});
1110-
bool AnyAsmPredicate = It != Predicates.end();
1111-
if (!AnyAsmPredicate)
1112-
return false;
1104+
if (It == Predicates.end())
1105+
return std::string();
1106+
1107+
std::string Predicate;
1108+
raw_string_ostream OS(Predicate);
11131109
SubtargetFeatureInfo::emitMCPredicateCheck(OS, TargetName, Predicates);
1114-
return true;
1110+
return Predicate;
11151111
}
11161112

11171113
static unsigned getPredicateIndex(StringRef Predicate,
@@ -1127,15 +1123,13 @@ static unsigned getPredicateIndex(StringRef Predicate,
11271123

11281124
void DecoderTableBuilder::emitPredicateTableEntry(unsigned EncodingID) const {
11291125
const InstructionEncoding &Encoding = Encodings[EncodingID];
1130-
// Build up the predicate string.
1131-
SmallString<256> Predicate;
1132-
raw_svector_ostream PS(Predicate);
1133-
if (!emitPredicateMatch(PS, Encoding, Target.getName()))
1126+
std::string Predicate = getPredicateString(Encoding, Target.getName());
1127+
if (Predicate.empty())
11341128
return;
11351129

11361130
// Figure out the index into the predicate table for the predicate just
11371131
// computed.
1138-
unsigned PIdx = getPredicateIndex(PS.str(), TableInfo);
1132+
unsigned PIdx = getPredicateIndex(Predicate, TableInfo);
11391133

11401134
TableInfo.Table.insertOpcode(OPC_CheckPredicate);
11411135
TableInfo.Table.insertULEB128(PIdx);

0 commit comments

Comments
 (0)