Index: llvm/trunk/utils/TableGen/GlobalISelEmitter.cpp =================================================================== --- llvm/trunk/utils/TableGen/GlobalISelEmitter.cpp +++ llvm/trunk/utils/TableGen/GlobalISelEmitter.cpp @@ -407,6 +407,8 @@ 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 @@ /// The currently defined labels. DenseMap 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 @@ MatchTableRecord::MTRF_CommaFollows); } - MatchTable(unsigned ID) : ID(ID), CurrentSize(0) {} + static MatchTable buildTable(ArrayRef Rules); + + MatchTable(unsigned ID = 0) : ID(ID) {} void push_back(const MatchTableRecord &Value) { if (Value.Flags & MatchTableRecord::MTRF_Label) @@ -474,7 +477,7 @@ 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 @@ } }; -unsigned MatchTable::CurrentLabelID = 0; - MatchTableRecord MatchTable::LineBreak = { None, "" /* Emit String */, 0 /* Elements */, MatchTableRecord::MTRF_LineBreakFollows}; @@ -577,6 +578,14 @@ virtual std::unique_ptr forgetFirstCondition() = 0; }; +MatchTable MatchTable::buildTable(ArrayRef Rules) { + MatchTable Table; + for (Matcher *Rule : Rules) + Rule->emit(Table); + + return Table << MatchTable::Opcode("GIM_Reject") << MatchTable::LineBreak; +} + class GroupMatcher : public Matcher { SmallVector, 8> Conditions; SmallVector Rules; @@ -2686,8 +2695,10 @@ /// # predicate C /// \endverbatim std::vector optimizeRules( - const std::vector &Rules, + ArrayRef Rules, std::vector> &StorageGroupMatcher); + + MatchTable buildMatchTable(MutableArrayRef Rules, bool Optimize); }; void GlobalISelEmitter::gatherNodeEquivs() { @@ -3644,7 +3655,7 @@ } std::vector GlobalISelEmitter::optimizeRules( - const std::vector &Rules, + ArrayRef Rules, std::vector> &StorageGroupMatcher) { std::vector OptRules; // Start with a stupid grouping for now. @@ -3675,6 +3686,23 @@ return OptRules; } +MatchTable +GlobalISelEmitter::buildMatchTable(MutableArrayRef Rules, + bool Optimize) { + std::vector InputRules; + for (Matcher &Rule : Rules) + InputRules.push_back(&Rule); + + if (!Optimize) + return MatchTable::buildTable(InputRules); + + std::vector> StorageGroupMatcher; + std::vector OptRules = + optimizeRules(InputRules, StorageGroupMatcher); + + return MatchTable::buildTable(OptRules); +} + void GlobalISelEmitter::run(raw_ostream &OS) { if (!UseCoverageFile.empty()) { RuleCoverage = CodeGenCoverage(); @@ -3941,21 +3969,6 @@ } return false; }); - std::vector> StorageGroupMatcher; - - std::vector InputRules; - for (Matcher &Rule : Rules) - InputRules.push_back(&Rule); - - std::vector 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 @@ << " return false;\n" << "}\n\n"; + const MatchTable Table = buildMatchTable(Rules, OptimizeMatchTable); OS << "const int64_t *" << Target.getName() << "InstructionSelector::getMatchTable() const {\n"; Table.emitDeclaration(OS);