Index: llvm/trunk/utils/TableGen/GlobalISelEmitter.cpp =================================================================== --- llvm/trunk/utils/TableGen/GlobalISelEmitter.cpp +++ llvm/trunk/utils/TableGen/GlobalISelEmitter.cpp @@ -110,16 +110,6 @@ //===- Matchers -----------------------------------------------------------===// -struct MatchAction { - virtual ~MatchAction() {} - virtual void emit(raw_ostream &OS) const = 0; -}; - -raw_ostream &operator<<(raw_ostream &S, const MatchAction &A) { - A.emit(S); - return S; -} - template class PredicateListMatcher { private: typedef std::vector> PredicateVec; @@ -293,11 +283,22 @@ } }; -struct MutateOpcode : public MatchAction { - MutateOpcode(const CodeGenInstruction *I) : I(I) {} +//===- Actions ------------------------------------------------------------===// + +class MatchAction { +public: + virtual ~MatchAction() {} + virtual void emitCxxActionStmts(raw_ostream &OS) const = 0; +}; + +class MutateOpcodeAction : public MatchAction { +private: const CodeGenInstruction *I; - virtual void emit(raw_ostream &OS) const { +public: + MutateOpcodeAction(const CodeGenInstruction *I) : I(I) {} + + virtual void emitCxxActionStmts(raw_ostream &OS) const { OS << "I.setDesc(TII.get(" << I->Namespace << "::" << I->TheDef->getName() << "));"; } @@ -312,9 +313,9 @@ const PatternToMatch &P; std::vector> Matchers; + std::vector> Actions; public: - std::vector> Actions; RuleMatcher(const PatternToMatch &P) : P(P) {} @@ -323,6 +324,12 @@ return *Matchers.back(); } + template + Kind &addAction(Args&&... args) { + Actions.emplace_back(llvm::make_unique(std::forward(args)...)); + return *static_cast(Actions.back().get()); + } + void emit(raw_ostream &OS) { if (Matchers.empty()) llvm_unreachable("Unexpected empty matcher!"); @@ -344,8 +351,11 @@ Matchers.front()->emitCxxPredicateExpr(OS, "I"); OS << ") {\n"; - for (auto &MA : Actions) - OS << " " << *MA << "\n"; + for (const auto &MA : Actions) { + OS << " "; + MA->emitCxxActionStmts(OS); + OS << "\n"; + } OS << " constrainSelectedInstRegOperands(I, TII, TRI, RBI);\n"; OS << " return true;\n"; @@ -410,7 +420,7 @@ // The operators look good: match the opcode and mutate it to the new one. InstructionMatcher &InsnMatcher = M.addInstructionMatcher(); InsnMatcher.addPredicate(&SrcGI); - M.Actions.emplace_back(new MutateOpcode(&DstI)); + M.addAction(&DstI); // Next, analyze the children, only accepting patterns that don't require // any change to operands.