diff options
-rw-r--r-- | llvm/utils/TableGen/GlobalISelEmitter.cpp | 62 |
1 files changed, 38 insertions, 24 deletions
diff --git a/llvm/utils/TableGen/GlobalISelEmitter.cpp b/llvm/utils/TableGen/GlobalISelEmitter.cpp index f4c253b2ade..d34bcfe6cf7 100644 --- a/llvm/utils/TableGen/GlobalISelEmitter.cpp +++ b/llvm/utils/TableGen/GlobalISelEmitter.cpp @@ -407,6 +407,8 @@ public: unsigned size() const { return NumElements; } }; +class Matcher; + /// Holds the contents of a generated MatchTable to enable formatting and the /// necessary index tracking needed to support GIM_Try. class MatchTable { @@ -419,10 +421,9 @@ class MatchTable { /// The currently defined labels. DenseMap<unsigned, unsigned> LabelMap; /// Tracks the sum of MatchTableRecord::NumElements as the table is built. - unsigned CurrentSize; - + unsigned CurrentSize = 0; /// A unique identifier for a MatchTable label. - static unsigned CurrentLabelID; + unsigned CurrentLabelID = 0; public: static MatchTableRecord LineBreak; @@ -465,7 +466,9 @@ public: MatchTableRecord::MTRF_CommaFollows); } - MatchTable(unsigned ID) : ID(ID), CurrentSize(0) {} + static MatchTable buildTable(ArrayRef<Matcher *> Rules); + + MatchTable(unsigned ID = 0) : ID(ID) {} void push_back(const MatchTableRecord &Value) { if (Value.Flags & MatchTableRecord::MTRF_Label) @@ -474,7 +477,7 @@ public: CurrentSize += Value.size(); } - unsigned allocateLabelID() const { return CurrentLabelID++; } + unsigned allocateLabelID() { return CurrentLabelID++; } void defineLabel(unsigned LabelID) { LabelMap.insert(std::make_pair(LabelID, CurrentSize)); @@ -519,8 +522,6 @@ public: } }; -unsigned MatchTable::CurrentLabelID = 0; - MatchTableRecord MatchTable::LineBreak = { None, "" /* Emit String */, 0 /* Elements */, MatchTableRecord::MTRF_LineBreakFollows}; @@ -577,6 +578,14 @@ public: virtual std::unique_ptr<PredicateMatcher> forgetFirstCondition() = 0; }; +MatchTable MatchTable::buildTable(ArrayRef<Matcher *> Rules) { + MatchTable Table; + for (Matcher *Rule : Rules) + Rule->emit(Table); + + return Table << MatchTable::Opcode("GIM_Reject") << MatchTable::LineBreak; +} + class GroupMatcher : public Matcher { SmallVector<std::unique_ptr<PredicateMatcher>, 8> Conditions; SmallVector<Matcher *, 8> Rules; @@ -2686,8 +2695,10 @@ private: /// # predicate C /// \endverbatim std::vector<Matcher *> optimizeRules( - const std::vector<Matcher *> &Rules, + ArrayRef<Matcher *> Rules, std::vector<std::unique_ptr<GroupMatcher>> &StorageGroupMatcher); + + MatchTable buildMatchTable(MutableArrayRef<RuleMatcher> Rules, bool Optimize); }; void GlobalISelEmitter::gatherNodeEquivs() { @@ -3644,7 +3655,7 @@ void GlobalISelEmitter::emitImmPredicates( } std::vector<Matcher *> GlobalISelEmitter::optimizeRules( - const std::vector<Matcher *> &Rules, + ArrayRef<Matcher *> Rules, std::vector<std::unique_ptr<GroupMatcher>> &StorageGroupMatcher) { std::vector<Matcher *> OptRules; // Start with a stupid grouping for now. @@ -3675,6 +3686,23 @@ std::vector<Matcher *> GlobalISelEmitter::optimizeRules( return OptRules; } +MatchTable +GlobalISelEmitter::buildMatchTable(MutableArrayRef<RuleMatcher> Rules, + bool Optimize) { + std::vector<Matcher *> InputRules; + for (Matcher &Rule : Rules) + InputRules.push_back(&Rule); + + if (!Optimize) + return MatchTable::buildTable(InputRules); + + std::vector<std::unique_ptr<GroupMatcher>> StorageGroupMatcher; + std::vector<Matcher *> OptRules = + optimizeRules(InputRules, StorageGroupMatcher); + + return MatchTable::buildTable(OptRules); +} + void GlobalISelEmitter::run(raw_ostream &OS) { if (!UseCoverageFile.empty()) { RuleCoverage = CodeGenCoverage(); @@ -3941,21 +3969,6 @@ void GlobalISelEmitter::run(raw_ostream &OS) { } return false; }); - std::vector<std::unique_ptr<GroupMatcher>> StorageGroupMatcher; - - std::vector<Matcher *> InputRules; - for (Matcher &Rule : Rules) - InputRules.push_back(&Rule); - - std::vector<Matcher *> OptRules = - OptimizeMatchTable ? optimizeRules(InputRules, StorageGroupMatcher) - : InputRules; - - MatchTable Table(0); - for (Matcher *Rule : OptRules) - Rule->emit(Table); - - Table << MatchTable::Opcode("GIM_Reject") << MatchTable::LineBreak; OS << "bool " << Target.getName() << "InstructionSelector::selectImpl(MachineInstr &I, CodeGenCoverage " @@ -3978,6 +3991,7 @@ void GlobalISelEmitter::run(raw_ostream &OS) { << " return false;\n" << "}\n\n"; + const MatchTable Table = buildMatchTable(Rules, OptimizeMatchTable); OS << "const int64_t *" << Target.getName() << "InstructionSelector::getMatchTable() const {\n"; Table.emitDeclaration(OS); |