Skip to content

Commit f19eacf

Browse files
committedMar 21, 2018
[TableGen] Use SmallMapVector to simplify some code that was trying to keep a vector unique
Summary: This code previously had a SmallVector of std::pairs containing an unsigned and another SmallVector. The outer vector was using the unsigned effectively as a key to decide which SmallVector to add into. So each time something new needed to be added the out vector needed to be scanned. If it wasn't found a new entry needed to be added to be added. This sounds very much like a map, but the next loop iterates over the outer vector to get a deterministic order. We can simplify this code greatly if use SmallMapVector instead. This uses more stack space since we now have a vector and a map, but the searching and creating new entries all happens behind the scenes. It should also make the search more efficient though usually there are only a few entries so that doesn't matter much. We could probably get determinism by just using std::map which would iterate over the unsigned key, but that would generate different output from what we get with the current implementation. Reviewers: RKSimon, dblaikie Reviewed By: dblaikie Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D44711 llvm-svn: 328070
1 parent 98c1aef commit f19eacf

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed
 

‎llvm/utils/TableGen/CodeGenSchedule.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "CodeGenSchedule.h"
1616
#include "CodeGenInstruction.h"
1717
#include "CodeGenTarget.h"
18+
#include "llvm/ADT/MapVector.h"
1819
#include "llvm/ADT/STLExtras.h"
1920
#include "llvm/ADT/SmallPtrSet.h"
2021
#include "llvm/ADT/SmallSet.h"
@@ -743,7 +744,7 @@ void CodeGenSchedModels::createInstRWClass(Record *InstRWDef) {
743744
// intersects with an existing class via a previous InstRWDef. Instrs that do
744745
// not intersect with an existing class refer back to their former class as
745746
// determined from ItinDef or SchedRW.
746-
SmallVector<std::pair<unsigned, SmallVector<Record *, 8>>, 4> ClassInstrs;
747+
SmallMapVector<unsigned, SmallVector<Record *, 8>, 4> ClassInstrs;
747748
// Sort Instrs into sets.
748749
const RecVec *InstDefs = Sets.expand(InstRWDef);
749750
if (InstDefs->empty())
@@ -754,22 +755,13 @@ void CodeGenSchedModels::createInstRWClass(Record *InstRWDef) {
754755
if (Pos == InstrClassMap.end())
755756
PrintFatalError(InstDef->getLoc(), "No sched class for instruction.");
756757
unsigned SCIdx = Pos->second;
757-
unsigned CIdx = 0, CEnd = ClassInstrs.size();
758-
for (; CIdx != CEnd; ++CIdx) {
759-
if (ClassInstrs[CIdx].first == SCIdx)
760-
break;
761-
}
762-
if (CIdx == CEnd) {
763-
ClassInstrs.resize(CEnd + 1);
764-
ClassInstrs[CIdx].first = SCIdx;
765-
}
766-
ClassInstrs[CIdx].second.push_back(InstDef);
758+
ClassInstrs[SCIdx].push_back(InstDef);
767759
}
768760
// For each set of Instrs, create a new class if necessary, and map or remap
769761
// the Instrs to it.
770-
for (unsigned CIdx = 0, CEnd = ClassInstrs.size(); CIdx != CEnd; ++CIdx) {
771-
unsigned OldSCIdx = ClassInstrs[CIdx].first;
772-
ArrayRef<Record*> InstDefs = ClassInstrs[CIdx].second;
762+
for (auto &Entry : ClassInstrs) {
763+
unsigned OldSCIdx = Entry.first;
764+
ArrayRef<Record*> InstDefs = Entry.second;
773765
// If the all instrs in the current class are accounted for, then leave
774766
// them mapped to their old class.
775767
if (OldSCIdx) {

0 commit comments

Comments
 (0)
Please sign in to comment.