Skip to content

Commit 4780658

Browse files
authored
[NFC][TableGen] DecoderEmitter optimize scope stack in Filter::emitTableEntry (llvm#135693)
- Create a new stack scope only in the fallthrough case. - For the non-fallthrough cases, any fixup entries will naturally be added to the existing scope without needing to copy them manually. - Verified that the generated `GenDisassembler` files are identical with and without this change.
1 parent 57025b4 commit 4780658

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

llvm/utils/TableGen/DecoderEmitter.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -714,30 +714,39 @@ void Filter::emitTableEntry(DecoderTableInfo &TableInfo) const {
714714
TableInfo.Table.insertULEB128(StartBit);
715715
TableInfo.Table.push_back(NumBits);
716716

717-
// A new filter entry begins a new scope for fixup resolution.
718-
TableInfo.FixupStack.emplace_back();
717+
// If the NO_FIXED_SEGMENTS_SENTINEL is present, we need to add a new scope
718+
// for this filter. Otherwise, we can skip adding a new scope and any
719+
// patching added will automatically be added to the enclosing scope.
720+
721+
// If NO_FIXED_SEGMENTS_SENTINEL is present, it will be last entry in
722+
// FilterChooserMap.
723+
724+
const uint64_t LastFilter = FilterChooserMap.rbegin()->first;
725+
bool HasFallthrough = LastFilter == NO_FIXED_SEGMENTS_SENTINEL;
726+
if (HasFallthrough)
727+
TableInfo.FixupStack.emplace_back();
719728

720729
DecoderTable &Table = TableInfo.Table;
721730

722731
size_t PrevFilter = 0;
723-
bool HasFallthrough = false;
724-
for (const auto &Filter : FilterChooserMap) {
725-
// Field value -1 implies a non-empty set of variable instructions.
726-
// See also recurse().
727-
if (Filter.first == NO_FIXED_SEGMENTS_SENTINEL) {
728-
HasFallthrough = true;
729-
732+
for (const auto &[FilterVal, Delegate] : FilterChooserMap) {
733+
// Field value NO_FIXED_SEGMENTS_SENTINEL implies a non-empty set of
734+
// variable instructions. See also recurse().
735+
if (FilterVal == NO_FIXED_SEGMENTS_SENTINEL) {
730736
// Each scope should always have at least one filter value to check
731737
// for.
732738
assert(PrevFilter != 0 && "empty filter set!");
733739
FixupList &CurScope = TableInfo.FixupStack.back();
734740
// Resolve any NumToSkip fixups in the current scope.
735741
resolveTableFixups(Table, CurScope, Table.size());
736-
CurScope.clear();
742+
743+
// Delete the scope we have added here.
744+
TableInfo.FixupStack.pop_back();
745+
737746
PrevFilter = 0; // Don't re-process the filter's fallthrough.
738747
} else {
739748
Table.push_back(MCD::OPC_FilterValue);
740-
Table.insertULEB128(Filter.first);
749+
Table.insertULEB128(FilterVal);
741750
// Reserve space for the NumToSkip entry. We'll backpatch the value
742751
// later.
743752
PrevFilter = Table.insertNumToSkip();
@@ -747,11 +756,11 @@ void Filter::emitTableEntry(DecoderTableInfo &TableInfo) const {
747756
// Now delegate to the sub filter chooser for further decodings.
748757
// The case may fallthrough, which happens if the remaining well-known
749758
// encoding bits do not match exactly.
750-
Filter.second->emitTableEntries(TableInfo);
759+
Delegate->emitTableEntries(TableInfo);
751760

752761
// Now that we've emitted the body of the handler, update the NumToSkip
753762
// of the filter itself to be able to skip forward when false. Subtract
754-
// two as to account for the width of the NumToSkip field itself.
763+
// three as to account for the width of the NumToSkip field itself.
755764
if (PrevFilter) {
756765
uint32_t NumToSkip = Table.size() - PrevFilter - 3;
757766
assert(isUInt<24>(NumToSkip) && "disassembler decoding table too large!");
@@ -761,13 +770,6 @@ void Filter::emitTableEntry(DecoderTableInfo &TableInfo) const {
761770
}
762771
}
763772

764-
// Any remaining unresolved fixups bubble up to the parent fixup scope.
765-
assert(TableInfo.FixupStack.size() > 1 && "fixup stack underflow!");
766-
FixupScopeList::iterator Source = TableInfo.FixupStack.end() - 1;
767-
FixupScopeList::iterator Dest = Source - 1;
768-
llvm::append_range(*Dest, *Source);
769-
TableInfo.FixupStack.pop_back();
770-
771773
// If there is no fallthrough, then the final filter should get fixed
772774
// up according to the enclosing scope rather than the current position.
773775
if (!HasFallthrough)

0 commit comments

Comments
 (0)