//===- GIMatchDag.h - Represent a DAG to be matched -----------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #ifndef LLVM_UTILS_TABLEGEN_GIMATCHDAG_H #define LLVM_UTILS_TABLEGEN_GIMATCHDAG_H #include "GIMatchDagEdge.h" #include "GIMatchDagInstr.h" #include "GIMatchDagOperands.h" #include "GIMatchDagPredicate.h" #include "GIMatchDagPredicateDependencyEdge.h" namespace llvm { /// This class manages lifetimes for data associated with the GIMatchDag object. class GIMatchDagContext { GIMatchDagOperandListContext OperandListCtx; public: const GIMatchDagOperandList &makeEmptyOperandList() { return OperandListCtx.makeEmptyOperandList(); } const GIMatchDagOperandList &makeOperandList(const CodeGenInstruction &I) { return OperandListCtx.makeOperandList(I); } const GIMatchDagOperandList &makeMIPredicateOperandList() { return OperandListCtx.makeMIPredicateOperandList(); } const GIMatchDagOperandList &makeTwoMOPredicateOperandList() { return OperandListCtx.makeTwoMOPredicateOperandList(); } void print(raw_ostream &OS) const { OperandListCtx.print(OS); } #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) LLVM_DUMP_METHOD void dump() const { print(errs()); } #endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) }; class GIMatchDag { public: using InstrNodesVec = std::vector>; using instr_node_iterator = raw_pointer_iterator; using const_instr_node_iterator = raw_pointer_iterator; using EdgesVec = std::vector>; using edge_iterator = raw_pointer_iterator; using const_edge_iterator = raw_pointer_iterator; using PredicateNodesVec = std::vector>; using predicate_iterator = raw_pointer_iterator; using const_predicate_iterator = raw_pointer_iterator; using PredicateDependencyEdgesVec = std::vector>; using predicate_edge_iterator = raw_pointer_iterator; using const_predicate_edge_iterator = raw_pointer_iterator; protected: GIMatchDagContext &Ctx; InstrNodesVec InstrNodes; PredicateNodesVec PredicateNodes; EdgesVec Edges; PredicateDependencyEdgesVec PredicateDependencies; std::vector MatchRoots; // FIXME: This is a temporary measure while we still accept arbitrary code // blocks to fix up the matcher while it's being developed. bool HasPostMatchPredicate = false; public: GIMatchDag(GIMatchDagContext &Ctx) : Ctx(Ctx) {} GIMatchDag(const GIMatchDag &) = delete; GIMatchDagContext &getContext() const { return Ctx; } edge_iterator edges_begin() { return raw_pointer_iterator(Edges.begin()); } edge_iterator edges_end() { return raw_pointer_iterator(Edges.end()); } const_edge_iterator edges_begin() const { return raw_pointer_iterator(Edges.begin()); } const_edge_iterator edges_end() const { return raw_pointer_iterator(Edges.end()); } iterator_range edges() { return make_range(edges_begin(), edges_end()); } iterator_range edges() const { return make_range(edges_begin(), edges_end()); } iterator_range::iterator> roots() { return make_range(MatchRoots.begin(), MatchRoots.end()); } iterator_range::const_iterator> roots() const { return make_range(MatchRoots.begin(), MatchRoots.end()); } instr_node_iterator instr_nodes_begin() { return raw_pointer_iterator(InstrNodes.begin()); } instr_node_iterator instr_nodes_end() { return raw_pointer_iterator(InstrNodes.end()); } const_instr_node_iterator instr_nodes_begin() const { return raw_pointer_iterator( InstrNodes.begin()); } const_instr_node_iterator instr_nodes_end() const { return raw_pointer_iterator( InstrNodes.end()); } iterator_range instr_nodes() { return make_range(instr_nodes_begin(), instr_nodes_end()); } iterator_range instr_nodes() const { return make_range(instr_nodes_begin(), instr_nodes_end()); } predicate_edge_iterator predicate_edges_begin() { return raw_pointer_iterator( PredicateDependencies.begin()); } predicate_edge_iterator predicate_edges_end() { return raw_pointer_iterator( PredicateDependencies.end()); } const_predicate_edge_iterator predicate_edges_begin() const { return raw_pointer_iterator( PredicateDependencies.begin()); } const_predicate_edge_iterator predicate_edges_end() const { return raw_pointer_iterator( PredicateDependencies.end()); } iterator_range predicate_edges() { return make_range(predicate_edges_begin(), predicate_edges_end()); } iterator_range predicate_edges() const { return make_range(predicate_edges_begin(), predicate_edges_end()); } predicate_iterator predicates_begin() { return raw_pointer_iterator( PredicateNodes.begin()); } predicate_iterator predicates_end() { return raw_pointer_iterator( PredicateNodes.end()); } const_predicate_iterator predicates_begin() const { return raw_pointer_iterator( PredicateNodes.begin()); } const_predicate_iterator predicates_end() const { return raw_pointer_iterator( PredicateNodes.end()); } iterator_range predicates() { return make_range(predicates_begin(), predicates_end()); } iterator_range predicates() const { return make_range(predicates_begin(), predicates_end()); } template GIMatchDagInstr *addInstrNode(Args &&... args) { auto Obj = std::make_unique(*this, std::forward(args)...); auto ObjRaw = Obj.get(); InstrNodes.push_back(std::move(Obj)); return ObjRaw; } template T *addPredicateNode(Args &&... args) { auto Obj = std::make_unique(getContext(), std::forward(args)...); auto ObjRaw = Obj.get(); PredicateNodes.push_back(std::move(Obj)); return ObjRaw; } template GIMatchDagEdge *addEdge(Args &&... args) { auto Obj = std::make_unique(std::forward(args)...); auto ObjRaw = Obj.get(); Edges.push_back(std::move(Obj)); return ObjRaw; } template GIMatchDagPredicateDependencyEdge *addPredicateDependency(Args &&... args) { auto Obj = std::make_unique( std::forward(args)...); auto ObjRaw = Obj.get(); PredicateDependencies.push_back(std::move(Obj)); return ObjRaw; } size_t getInstrNodeIdx(instr_node_iterator I) { return std::distance(instr_nodes_begin(), I); } size_t getInstrNodeIdx(const_instr_node_iterator I) const { return std::distance(instr_nodes_begin(), I); } size_t getNumInstrNodes() const { return InstrNodes.size(); } size_t getNumEdges() const { return Edges.size(); } size_t getNumPredicates() const { return PredicateNodes.size(); } void setHasPostMatchPredicate(bool V) { HasPostMatchPredicate = V; } bool hasPostMatchPredicate() const { return HasPostMatchPredicate; } void addMatchRoot(GIMatchDagInstr *N) { MatchRoots.push_back(N); } LLVM_DUMP_METHOD void print(raw_ostream &OS) const; #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) LLVM_DUMP_METHOD void dump() const { print(errs()); } #endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) void writeDOTGraph(raw_ostream &OS, StringRef ID) const; }; raw_ostream &operator<<(raw_ostream &OS, const GIMatchDag &G); } // end namespace llvm #endif // ifndef LLVM_UTILS_TABLEGEN_GIMATCHDAG_H