[Dominators] Simplify templates

Summary: DominatorTreeBase and related classes used overcomplicated template machinery. This patch simplifies them and gets rid of DominatorTreeBaseTraits and DominatorTreeBaseByTraits, which weren't actually used outside the DomTree construction.

Reviewers: dberlin, sanjoy, davide, grosser

Reviewed By: dberlin, davide, grosser

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D35285

llvm-svn: 307953
This commit is contained in:
Jakub Kuderski 2017-07-13 20:45:32 +00:00
parent 4284ed0894
commit 5af07f5c1e
4 changed files with 36 additions and 79 deletions

View File

@ -37,19 +37,11 @@ extern template class DomTreeNodeBase<BasicBlock>;
extern template class DominatorTreeBase<BasicBlock>;
namespace DomTreeBuilder {
extern template void Calculate<Function, BasicBlock *>(
DominatorTreeBaseByGraphTraits<GraphTraits<BasicBlock *>> &DT, Function &F);
using BBDomTree = DominatorTreeBase<BasicBlock>;
extern template void Calculate<Function, Inverse<BasicBlock *>>(
DominatorTreeBaseByGraphTraits<GraphTraits<Inverse<BasicBlock *>>> &DT,
Function &F);
extern template void Calculate<BBDomTree, Function>(BBDomTree &DT, Function &F);
extern template bool Verify<BasicBlock *>(
const DominatorTreeBaseByGraphTraits<GraphTraits<BasicBlock *>> &DT);
extern template bool Verify<Inverse<BasicBlock *>>(
const DominatorTreeBaseByGraphTraits<GraphTraits<Inverse<BasicBlock *>>>
&DT);
extern template bool Verify<BBDomTree>(const BBDomTree &DT);
} // namespace DomTreeBuilder
using DomTreeNode = DomTreeNodeBase<BasicBlock>;

View File

@ -43,25 +43,10 @@ namespace llvm {
template <class NodeT> class DominatorTreeBase;
namespace detail {
template <typename GT> struct DominatorTreeBaseTraits {
static_assert(std::is_pointer<typename GT::NodeRef>::value,
"Currently NodeRef must be a pointer type.");
using type = DominatorTreeBase<
typename std::remove_pointer<typename GT::NodeRef>::type>;
};
} // end namespace detail
template <typename GT>
using DominatorTreeBaseByGraphTraits =
typename detail::DominatorTreeBaseTraits<GT>::type;
/// \brief Base class for the actual dominator tree node.
template <class NodeT> class DomTreeNodeBase {
friend struct PostDominatorTree;
template <class N> friend class DominatorTreeBase;
friend class DominatorTreeBase<NodeT>;
NodeT *TheBB;
DomTreeNodeBase *IDom;
@ -192,16 +177,16 @@ void PrintDomTree(const DomTreeNodeBase<NodeT> *N, raw_ostream &O,
}
namespace DomTreeBuilder {
template <class NodeT>
template <typename DomTreeT>
struct SemiNCAInfo;
// The calculate routine is provided in a separate header but referenced here.
template <class FuncT, class N>
void Calculate(DominatorTreeBaseByGraphTraits<GraphTraits<N>> &DT, FuncT &F);
template <typename DomTreeT, typename FuncT>
void Calculate(DomTreeT &DT, FuncT &F);
// The verify function is provided in a separate header but referenced here.
template <class N>
bool Verify(const DominatorTreeBaseByGraphTraits<GraphTraits<N>> &DT);
template <class DomTreeT>
bool Verify(const DomTreeT &DT);
} // namespace DomTreeBuilder
/// \brief Core dominator tree base class.
@ -221,10 +206,13 @@ template <class NodeT> class DominatorTreeBase {
mutable bool DFSInfoValid = false;
mutable unsigned int SlowQueries = 0;
friend struct DomTreeBuilder::SemiNCAInfo<NodeT>;
using SNCAInfoTy = DomTreeBuilder::SemiNCAInfo<NodeT>;
friend struct DomTreeBuilder::SemiNCAInfo<DominatorTreeBase>;
public:
static_assert(std::is_pointer<typename GraphTraits<NodeT *>::NodeRef>::value,
"Currently DominatorTreeBase supports only pointer nodes");
using NodeType = NodeT;
using NodePtr = NodeT *;
explicit DominatorTreeBase(bool isPostDom) : IsPostDominators(isPostDom) {}
DominatorTreeBase(DominatorTreeBase &&Arg)
@ -612,35 +600,29 @@ public:
using TraitsTy = GraphTraits<FT *>;
reset();
if (!this->IsPostDominators) {
if (!IsPostDominators) {
// Initialize root
NodeT *entry = TraitsTy::getEntryNode(&F);
addRoot(entry);
DomTreeBuilder::Calculate<FT, NodeT *>(*this, F);
} else {
// Initialize the roots list
for (auto *Node : nodes(&F))
if (TraitsTy::child_begin(Node) == TraitsTy::child_end(Node))
addRoot(Node);
DomTreeBuilder::Calculate<FT, Inverse<NodeT *>>(*this, F);
}
DomTreeBuilder::Calculate(*this, F);
}
/// verify - check parent and sibling property
bool verify() const {
return this->isPostDominator()
? DomTreeBuilder::Verify<Inverse<NodeT *>>(*this)
: DomTreeBuilder::Verify<NodeT *>(*this);
}
bool verify() const { return DomTreeBuilder::Verify(*this); }
protected:
void addRoot(NodeT *BB) { this->Roots.push_back(BB); }
void reset() {
DomTreeNodes.clear();
this->Roots.clear();
Roots.clear();
RootNode = nullptr;
DFSInfoValid = false;
SlowQueries = 0;

View File

@ -49,13 +49,13 @@ struct ChildrenGetter<NodePtr, true> {
}
};
// Information record used by Semi-NCA during tree construction.
template <typename NodeT>
template <typename DomTreeT>
struct SemiNCAInfo {
using NodePtr = NodeT *;
using DomTreeT = DominatorTreeBase<NodeT>;
using NodePtr = typename DomTreeT::NodePtr;
using NodeT = typename DomTreeT::NodeType;
using TreeNodePtr = DomTreeNodeBase<NodeT> *;
// Information record used by Semi-NCA during tree construction.
struct InfoRec {
unsigned DFSNum = 0;
unsigned Parent = 0;
@ -524,23 +524,16 @@ struct SemiNCAInfo {
}
};
template <class FuncT, class NodeT>
void Calculate(DominatorTreeBaseByGraphTraits<GraphTraits<NodeT>> &DT,
FuncT &F) {
using NodePtr = typename GraphTraits<NodeT>::NodeRef;
static_assert(std::is_pointer<NodePtr>::value,
"NodePtr should be a pointer type");
SemiNCAInfo<typename std::remove_pointer<NodePtr>::type> SNCA;
template <class DomTreeT, class FuncT>
void Calculate(DomTreeT &DT, FuncT &F) {
SemiNCAInfo<DomTreeT> SNCA;
SNCA.calculateFromScratch(DT, GraphTraits<FuncT *>::size(&F));
}
template <class NodeT>
bool Verify(const DominatorTreeBaseByGraphTraits<GraphTraits<NodeT>> &DT) {
using NodePtr = typename GraphTraits<NodeT>::NodeRef;
static_assert(std::is_pointer<NodePtr>::value,
"NodePtr should be a pointer type");
SemiNCAInfo<typename std::remove_pointer<NodePtr>::type> SNCA;
template <class DomTreeT>
bool Verify(const DomTreeT &DT) {
SemiNCAInfo<DomTreeT> SNCA;
return SNCA.verifyReachability(DT) && SNCA.VerifyLevels(DT) &&
SNCA.verifyNCD(DT) && SNCA.verifyParentProperty(DT) &&
SNCA.verifySiblingProperty(DT);

View File

@ -63,22 +63,12 @@ bool BasicBlockEdge::isSingleEdge() const {
template class llvm::DomTreeNodeBase<BasicBlock>;
template class llvm::DominatorTreeBase<BasicBlock>;
template void llvm::DomTreeBuilder::Calculate<Function, BasicBlock *>(
DominatorTreeBase<
typename std::remove_pointer<GraphTraits<BasicBlock *>::NodeRef>::type>
&DT,
Function &F);
template void llvm::DomTreeBuilder::Calculate<Function, Inverse<BasicBlock *>>(
DominatorTreeBase<typename std::remove_pointer<
GraphTraits<Inverse<BasicBlock *>>::NodeRef>::type> &DT,
Function &F);
template bool llvm::DomTreeBuilder::Verify<BasicBlock *>(
const DominatorTreeBase<
typename std::remove_pointer<GraphTraits<BasicBlock *>::NodeRef>::type>
&DT);
template bool llvm::DomTreeBuilder::Verify<Inverse<BasicBlock *>>(
const DominatorTreeBase<typename std::remove_pointer<
GraphTraits<Inverse<BasicBlock *>>::NodeRef>::type> &DT);
template void
llvm::DomTreeBuilder::Calculate<DomTreeBuilder::BBDomTree, Function>(
DomTreeBuilder::BBDomTree &DT, Function &F);
template bool llvm::DomTreeBuilder::Verify<DomTreeBuilder::BBDomTree>(
const DomTreeBuilder::BBDomTree &DT);
bool DominatorTree::invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &) {