Skip to content

Commit fc6024d

Browse files
authored
[TableGen][DecoderEmitter] Shrink lifetime of Filters vector (NFC) (llvm#153998)
Only one element of the `Filters` vector (see `BestIndex`) is used outside the method that fills it. Localize the vector to the method, replacing the member variable with the only used element. Part of an effort to simplify DecoderEmitter code.
1 parent ee51f35 commit fc6024d

File tree

1 file changed

+34
-43
lines changed

1 file changed

+34
-43
lines changed

llvm/utils/TableGen/DecoderEmitter.cpp

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,8 @@ class FilterChooser {
487487
// Lookup table for the operand decoding of instructions.
488488
const std::map<unsigned, std::vector<OperandInfo>> &Operands;
489489

490-
// Vector of candidate filters.
491-
std::vector<Filter> Filters;
490+
// The selected filter, if any.
491+
std::unique_ptr<Filter> BestFilter;
492492

493493
// Array of bit values passed down from our parent.
494494
// Set to all BIT_UNFILTERED's for Parent == NULL.
@@ -497,9 +497,6 @@ class FilterChooser {
497497
// Links to the FilterChooser above us in the decoding tree.
498498
const FilterChooser *Parent;
499499

500-
// Index of the best filter from Filters.
501-
int BestIndex;
502-
503500
// Width of instructions
504501
unsigned BitWidth;
505502

@@ -519,7 +516,7 @@ class FilterChooser {
519516
unsigned BW, const DecoderEmitter *E)
520517
: AllInstructions(Insts), Opcodes(IDs), Operands(Ops),
521518
FilterBitValues(BW, BitValue::BIT_UNFILTERED), Parent(nullptr),
522-
BestIndex(-1), BitWidth(BW), Emitter(E) {
519+
BitWidth(BW), Emitter(E) {
523520
doFilter();
524521
}
525522

@@ -529,7 +526,7 @@ class FilterChooser {
529526
const std::vector<BitValue> &ParentFilterBitValues,
530527
const FilterChooser &parent)
531528
: AllInstructions(Insts), Opcodes(IDs), Operands(Ops),
532-
FilterBitValues(ParentFilterBitValues), Parent(&parent), BestIndex(-1),
529+
FilterBitValues(ParentFilterBitValues), Parent(&parent),
533530
BitWidth(parent.BitWidth), Emitter(parent.Emitter) {
534531
doFilter();
535532
}
@@ -578,11 +575,6 @@ class FilterChooser {
578575
/// dumpFilterArray on each filter chooser up to the top level one.
579576
void dumpStack(raw_ostream &OS, const char *prefix) const;
580577

581-
Filter &bestFilter() {
582-
assert(BestIndex != -1 && "BestIndex not set");
583-
return Filters[BestIndex];
584-
}
585-
586578
bool PositionFiltered(unsigned Idx) const {
587579
return FilterBitValues[Idx].isSet();
588580
}
@@ -625,8 +617,8 @@ class FilterChooser {
625617

626618
// reportRegion is a helper function for filterProcessor to mark a region as
627619
// eligible for use as a filter region.
628-
void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex,
629-
bool AllowMixed);
620+
void reportRegion(std::vector<std::unique_ptr<Filter>> &Filters, bitAttr_t RA,
621+
unsigned StartBit, unsigned BitIndex, bool AllowMixed);
630622

631623
// FilterProcessor scans the well-known encoding bits of the instructions and
632624
// builds up a list of candidate filters. It chooses the best filter and
@@ -1522,28 +1514,25 @@ void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
15221514
// Assign a single filter and run with it. Top level API client can initialize
15231515
// with a single filter to start the filtering process.
15241516
void FilterChooser::runSingleFilter(unsigned startBit, unsigned numBit) {
1525-
Filters.clear();
1526-
Filters.emplace_back(*this, startBit, numBit);
1527-
BestIndex = 0; // Sole Filter instance to choose from.
1528-
bestFilter().recurse();
1517+
BestFilter = std::make_unique<Filter>(*this, startBit, numBit);
1518+
BestFilter->recurse();
15291519
}
15301520

15311521
// reportRegion is a helper function for filterProcessor to mark a region as
15321522
// eligible for use as a filter region.
1533-
void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
1523+
void FilterChooser::reportRegion(std::vector<std::unique_ptr<Filter>> &Filters,
1524+
bitAttr_t RA, unsigned StartBit,
15341525
unsigned BitIndex, bool AllowMixed) {
15351526
if (AllowMixed ? RA == ATTR_MIXED : RA == ATTR_ALL_SET)
1536-
Filters.emplace_back(*this, StartBit, BitIndex - StartBit);
1527+
Filters.push_back(
1528+
std::make_unique<Filter>(*this, StartBit, BitIndex - StartBit));
15371529
}
15381530

15391531
// FilterProcessor scans the well-known encoding bits of the instructions and
15401532
// builds up a list of candidate filters. It chooses the best filter and
15411533
// recursively descends down the decoding tree.
15421534
bool FilterChooser::filterProcessor(ArrayRef<bitAttr_t> BitAttrs,
15431535
bool AllowMixed, bool Greedy) {
1544-
Filters.clear();
1545-
BestIndex = -1;
1546-
15471536
assert(Opcodes.size() >= 2 && "Nothing to filter");
15481537

15491538
// Heuristics. See also doFilter()'s "Heuristics" comment when num of
@@ -1587,6 +1576,7 @@ bool FilterChooser::filterProcessor(ArrayRef<bitAttr_t> BitAttrs,
15871576
bitAttr_t RA = ATTR_NONE;
15881577
unsigned StartBit = 0;
15891578

1579+
std::vector<std::unique_ptr<Filter>> Filters;
15901580
for (unsigned BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
15911581
bitAttr_t bitAttr = BitAttrs[BitIndex];
15921582

@@ -1614,17 +1604,17 @@ bool FilterChooser::filterProcessor(ArrayRef<bitAttr_t> BitAttrs,
16141604
case ATTR_ALL_SET:
16151605
switch (bitAttr) {
16161606
case ATTR_FILTERED:
1617-
reportRegion(RA, StartBit, BitIndex, AllowMixed);
1607+
reportRegion(Filters, RA, StartBit, BitIndex, AllowMixed);
16181608
RA = ATTR_NONE;
16191609
break;
16201610
case ATTR_ALL_SET:
16211611
break;
16221612
case ATTR_ALL_UNSET:
1623-
reportRegion(RA, StartBit, BitIndex, AllowMixed);
1613+
reportRegion(Filters, RA, StartBit, BitIndex, AllowMixed);
16241614
RA = ATTR_NONE;
16251615
break;
16261616
case ATTR_MIXED:
1627-
reportRegion(RA, StartBit, BitIndex, AllowMixed);
1617+
reportRegion(Filters, RA, StartBit, BitIndex, AllowMixed);
16281618
StartBit = BitIndex;
16291619
RA = ATTR_MIXED;
16301620
break;
@@ -1635,17 +1625,17 @@ bool FilterChooser::filterProcessor(ArrayRef<bitAttr_t> BitAttrs,
16351625
case ATTR_MIXED:
16361626
switch (bitAttr) {
16371627
case ATTR_FILTERED:
1638-
reportRegion(RA, StartBit, BitIndex, AllowMixed);
1628+
reportRegion(Filters, RA, StartBit, BitIndex, AllowMixed);
16391629
StartBit = BitIndex;
16401630
RA = ATTR_NONE;
16411631
break;
16421632
case ATTR_ALL_SET:
1643-
reportRegion(RA, StartBit, BitIndex, AllowMixed);
1633+
reportRegion(Filters, RA, StartBit, BitIndex, AllowMixed);
16441634
StartBit = BitIndex;
16451635
RA = ATTR_ALL_SET;
16461636
break;
16471637
case ATTR_ALL_UNSET:
1648-
reportRegion(RA, StartBit, BitIndex, AllowMixed);
1638+
reportRegion(Filters, RA, StartBit, BitIndex, AllowMixed);
16491639
RA = ATTR_NONE;
16501640
break;
16511641
case ATTR_MIXED:
@@ -1668,23 +1658,23 @@ bool FilterChooser::filterProcessor(ArrayRef<bitAttr_t> BitAttrs,
16681658
case ATTR_FILTERED:
16691659
break;
16701660
case ATTR_ALL_SET:
1671-
reportRegion(RA, StartBit, BitWidth, AllowMixed);
1661+
reportRegion(Filters, RA, StartBit, BitWidth, AllowMixed);
16721662
break;
16731663
case ATTR_ALL_UNSET:
16741664
break;
16751665
case ATTR_MIXED:
1676-
reportRegion(RA, StartBit, BitWidth, AllowMixed);
1666+
reportRegion(Filters, RA, StartBit, BitWidth, AllowMixed);
16771667
break;
16781668
}
16791669

16801670
// We have finished with the filter processings. Now it's time to choose
16811671
// the best performing filter.
1682-
BestIndex = 0;
1672+
unsigned BestIndex = 0;
16831673
bool AllUseless = true;
16841674
unsigned BestScore = 0;
16851675

16861676
for (const auto &[Idx, Filter] : enumerate(Filters)) {
1687-
unsigned Usefulness = Filter.usefulness();
1677+
unsigned Usefulness = Filter->usefulness();
16881678

16891679
if (Usefulness)
16901680
AllUseless = false;
@@ -1695,10 +1685,13 @@ bool FilterChooser::filterProcessor(ArrayRef<bitAttr_t> BitAttrs,
16951685
}
16961686
}
16971687

1698-
if (!AllUseless)
1699-
bestFilter().recurse();
1688+
if (AllUseless)
1689+
return false;
1690+
1691+
BestFilter = std::move(Filters[BestIndex]);
1692+
BestFilter->recurse();
1693+
return true;
17001694

1701-
return !AllUseless;
17021695
} // end of FilterChooser::filterProcessor(bool)
17031696

17041697
// Decides on the best configuration of filter(s) to use in order to decode
@@ -1779,8 +1772,7 @@ void FilterChooser::doFilter() {
17791772
return;
17801773

17811774
// If we come to here, the instruction decoding has failed.
1782-
// Set the BestIndex to -1 to indicate so.
1783-
BestIndex = -1;
1775+
assert(!BestFilter);
17841776
}
17851777

17861778
// emitTableEntries - Emit state machine entries to decode our share of
@@ -1795,12 +1787,11 @@ void FilterChooser::emitTableEntries(DecoderTableInfo &TableInfo) const {
17951787
}
17961788

17971789
// Choose the best filter to do the decodings!
1798-
if (BestIndex != -1) {
1799-
const Filter &Best = Filters[BestIndex];
1800-
if (Best.getNumFiltered() == 1)
1801-
emitSingletonTableEntry(TableInfo, Best);
1790+
if (BestFilter) {
1791+
if (BestFilter->getNumFiltered() == 1)
1792+
emitSingletonTableEntry(TableInfo, *BestFilter);
18021793
else
1803-
Best.emitTableEntry(TableInfo);
1794+
BestFilter->emitTableEntry(TableInfo);
18041795
return;
18051796
}
18061797

0 commit comments

Comments
 (0)