Skip to content

Commit 6f7e940

Browse files
authored
[TableGen] More efficiency improvements for encode/decode emission. (#84647)
DecoderEmitter and CodeEmitterGen perform repeated linear walks over the entire instruction list. This patch eliminates two more such walks. The eliminated traversals visit every instruction merely to determine whether the target has variable length encodings. For a target with variable length encodings, the original any_of will terminate quickly. But all targets other than M68k use fixed length encodings and thus any_of must visit the entire instruction list.
1 parent 2f1873d commit 6f7e940

File tree

5 files changed

+23
-20
lines changed

5 files changed

+23
-20
lines changed

llvm/utils/TableGen/CodeEmitterGen.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,7 @@ void CodeEmitterGen::run(raw_ostream &o) {
434434
ArrayRef<const CodeGenInstruction *> NumberedInstructions =
435435
Target.getInstructionsByEnumValue();
436436

437-
if (any_of(NumberedInstructions, [](const CodeGenInstruction *CGI) {
438-
Record *R = CGI->TheDef;
439-
return R->getValue("Inst") && isa<DagInit>(R->getValueInit("Inst"));
440-
})) {
437+
if (Target.hasVariableLengthEncodings()) {
441438
emitVarLenCodeEmitter(Records, o);
442439
} else {
443440
const CodeGenHwModes &HWM = Target.getHwModes();

llvm/utils/TableGen/CodeGenInstruction.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717
#include "llvm/ADT/StringMap.h"
1818
#include "llvm/ADT/StringRef.h"
1919
#include "llvm/CodeGenTypes/MachineValueType.h"
20+
#include "llvm/TableGen/Record.h"
2021
#include <cassert>
2122
#include <string>
2223
#include <utility>
2324
#include <vector>
2425

2526
namespace llvm {
26-
class Record;
27-
class DagInit;
2827
class CodeGenTarget;
2928

3029
class CGIOperandList {
@@ -333,6 +332,12 @@ class CodeGenInstruction {
333332
return isOperandImpl("InOperandList", i, "IsImmediate");
334333
}
335334

335+
/// Return true if the instruction uses a variable length encoding.
336+
bool isVariableLengthEncoding() const {
337+
const RecordVal *RV = TheDef->getValue("Inst");
338+
return RV && isa<DagInit>(RV->getValue());
339+
}
340+
336341
private:
337342
bool isOperandImpl(StringRef OpListName, unsigned i,
338343
StringRef PropertyName) const;

llvm/utils/TableGen/CodeGenTarget.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,11 @@ void CodeGenTarget::ReadInstructions() const {
480480
PrintFatalError("No 'Instruction' subclasses defined!");
481481

482482
// Parse the instructions defined in the .td file.
483-
for (unsigned i = 0, e = Insts.size(); i != e; ++i)
484-
Instructions[Insts[i]] = std::make_unique<CodeGenInstruction>(Insts[i]);
483+
for (Record *R : Insts) {
484+
Instructions[R] = std::make_unique<CodeGenInstruction>(R);
485+
if (Instructions[R]->isVariableLengthEncoding())
486+
HasVariableLengthEncodings = true;
487+
}
485488
}
486489

487490
static const CodeGenInstruction *GetInstByName(

llvm/utils/TableGen/CodeGenTarget.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class CodeGenTarget {
6565
mutable SmallVector<ValueTypeByHwMode, 8> LegalValueTypes;
6666
CodeGenHwModes CGH;
6767
std::vector<Record *> MacroFusions;
68+
mutable bool HasVariableLengthEncodings = false;
6869

6970
void ReadRegAltNameIndices() const;
7071
void ReadInstructions() const;
@@ -209,6 +210,9 @@ class CodeGenTarget {
209210
}
210211
inst_iterator inst_end() const { return getInstructionsByEnumValue().end(); }
211212

213+
/// Return whether instructions have variable length encodings on this target.
214+
bool hasVariableLengthEncodings() const { return HasVariableLengthEncodings; }
215+
212216
/// isLittleEndianEncoding - are instruction bit patterns defined as [0..n]?
213217
///
214218
bool isLittleEndianEncoding() const;

llvm/utils/TableGen/DecoderEmitter.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,8 +2499,8 @@ void DecoderEmitter::run(raw_ostream &o) {
24992499
const auto &NumberedInstructions = Target.getInstructionsByEnumValue();
25002500
NumberedEncodings.reserve(NumberedInstructions.size());
25012501
for (const auto &NumberedInstruction : NumberedInstructions) {
2502-
if (const RecordVal *RV =
2503-
NumberedInstruction->TheDef->getValue("EncodingInfos")) {
2502+
const Record *InstDef = NumberedInstruction->TheDef;
2503+
if (const RecordVal *RV = InstDef->getValue("EncodingInfos")) {
25042504
if (DefInit *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
25052505
EncodingInfoByHwMode EBM(DI->getDef(), HWM);
25062506
for (auto &KV : EBM)
@@ -2513,12 +2513,11 @@ void DecoderEmitter::run(raw_ostream &o) {
25132513
// This instruction is encoded the same on all HwModes. Emit it for all
25142514
// HwModes by default, otherwise leave it in a single common table.
25152515
if (DecoderEmitterSuppressDuplicates) {
2516-
NumberedEncodings.emplace_back(NumberedInstruction->TheDef,
2517-
NumberedInstruction, "AllModes");
2516+
NumberedEncodings.emplace_back(InstDef, NumberedInstruction, "AllModes");
25182517
} else {
25192518
for (StringRef HwModeName : HwModeNames)
2520-
NumberedEncodings.emplace_back(NumberedInstruction->TheDef,
2521-
NumberedInstruction, HwModeName);
2519+
NumberedEncodings.emplace_back(InstDef, NumberedInstruction,
2520+
HwModeName);
25222521
}
25232522
}
25242523
for (const auto &NumberedAlias :
@@ -2531,12 +2530,7 @@ void DecoderEmitter::run(raw_ostream &o) {
25312530
OpcMap;
25322531
std::map<unsigned, std::vector<OperandInfo>> Operands;
25332532
std::vector<unsigned> InstrLen;
2534-
2535-
bool IsVarLenInst =
2536-
any_of(NumberedInstructions, [](const CodeGenInstruction *CGI) {
2537-
RecordVal *RV = CGI->TheDef->getValue("Inst");
2538-
return RV && isa<DagInit>(RV->getValue());
2539-
});
2533+
bool IsVarLenInst = Target.hasVariableLengthEncodings();
25402534
unsigned MaxInstLen = 0;
25412535

25422536
for (unsigned i = 0; i < NumberedEncodings.size(); ++i) {

0 commit comments

Comments
 (0)