[TableGen][DAGISel] Dedup predicates with same code to run. NFCI.

I locally hit the 255 limit, but a lot of these are redundant: each
predicate coming from a different record was allocated a new number,
even when we already emitted the same code for another predicate.

Instead, re-use numbers and emit the predicate code only once.
This reduces the total text size of *DAGISel.cpp.o by ~1%.

llvm-svn: 246208
This commit is contained in:
Ahmed Bougacha 2015-08-27 20:43:34 +00:00
parent 8f88546575
commit 199fa13fff
1 changed files with 25 additions and 4 deletions

View File

@ -16,6 +16,7 @@
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/TableGen/Record.h"
@ -36,6 +37,10 @@ class MatcherTableEmitter {
DenseMap<TreePattern *, unsigned> NodePredicateMap;
std::vector<TreePredicateFn> NodePredicates;
// We de-duplicate the predicates by code string, and use this map to track
// all the patterns with "identical" predicates.
StringMap<TinyPtrVector<TreePattern *>> NodePredicatesByCodeToRun;
StringMap<unsigned> PatternPredicateMap;
std::vector<std::string> PatternPredicates;
@ -62,10 +67,23 @@ private:
formatted_raw_ostream &OS);
unsigned getNodePredicate(TreePredicateFn Pred) {
unsigned &Entry = NodePredicateMap[Pred.getOrigPatFragRecord()];
TreePattern *TP = Pred.getOrigPatFragRecord();
unsigned &Entry = NodePredicateMap[TP];
if (Entry == 0) {
NodePredicates.push_back(Pred);
Entry = NodePredicates.size();
TinyPtrVector<TreePattern *> &SameCodePreds =
NodePredicatesByCodeToRun[Pred.getCodeToRunOnSDNode()];
if (SameCodePreds.empty()) {
// We've never seen a predicate with the same code: allocate an entry.
NodePredicates.push_back(Pred);
Entry = NodePredicates.size();
} else {
// We did see an identical predicate: re-use it.
Entry = NodePredicateMap[SameCodePreds.front()];
assert(Entry != 0);
}
// In both cases, we've never seen this particular predicate before, so
// mark it in the list of predicates sharing the same code.
SameCodePreds.push_back(TP);
}
return Entry-1;
}
@ -635,7 +653,10 @@ void MatcherTableEmitter::EmitPredicateFunctions(formatted_raw_ostream &OS) {
TreePredicateFn PredFn = NodePredicates[i];
assert(!PredFn.isAlwaysTrue() && "No code in this predicate");
OS << " case " << i << ": { // " << NodePredicates[i].getFnName() <<'\n';
OS << " case " << i << ": { \n";
for (auto *SimilarPred :
NodePredicatesByCodeToRun[PredFn.getCodeToRunOnSDNode()])
OS << " // " << TreePredicateFn(SimilarPred).getFnName() <<'\n';
OS << PredFn.getCodeToRunOnSDNode() << "\n }\n";
}