2014-01-13 10:26:24 +01:00
|
|
|
//===- Dominators.h - Dominator Info Calculation ----------------*- C++ -*-===//
|
2005-04-21 22:19:05 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:19:05 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-02 07:45:17 +02:00
|
|
|
//
|
2011-01-02 23:09:33 +01:00
|
|
|
// This file defines the DominatorTree class, which provides fast and efficient
|
|
|
|
// dominance queries.
|
2005-04-21 22:19:05 +02:00
|
|
|
//
|
2001-07-02 07:45:17 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-01-13 10:26:24 +01:00
|
|
|
#ifndef LLVM_IR_DOMINATORS_H
|
|
|
|
#define LLVM_IR_DOMINATORS_H
|
2001-07-02 07:45:17 +02:00
|
|
|
|
2016-07-11 06:37:53 +02:00
|
|
|
#include "llvm/ADT/DenseMapInfo.h"
|
2007-10-23 23:04:37 +02:00
|
|
|
#include "llvm/ADT/GraphTraits.h"
|
2007-10-16 21:59:25 +02:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2014-03-04 12:45:46 +01:00
|
|
|
#include "llvm/IR/CFG.h"
|
2016-02-26 11:02:04 +01:00
|
|
|
#include "llvm/IR/PassManager.h"
|
2012-12-03 18:02:12 +01:00
|
|
|
#include "llvm/Pass.h"
|
2014-03-04 11:07:28 +01:00
|
|
|
#include "llvm/Support/GenericDomTree.h"
|
2003-06-30 23:59:07 +02:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
namespace llvm {
|
|
|
|
|
2016-04-16 09:51:28 +02:00
|
|
|
class Function;
|
|
|
|
class BasicBlock;
|
|
|
|
class raw_ostream;
|
|
|
|
|
2015-07-13 19:21:31 +02:00
|
|
|
extern template class DomTreeNodeBase<BasicBlock>;
|
|
|
|
extern template class DominatorTreeBase<BasicBlock>;
|
|
|
|
|
|
|
|
extern template void Calculate<Function, BasicBlock *>(
|
2016-08-17 22:01:58 +02:00
|
|
|
DominatorTreeBaseByGraphTraits<GraphTraits<BasicBlock *>> &DT, Function &F);
|
2015-07-13 19:21:31 +02:00
|
|
|
extern template void Calculate<Function, Inverse<BasicBlock *>>(
|
2016-08-17 22:01:58 +02:00
|
|
|
DominatorTreeBaseByGraphTraits<GraphTraits<Inverse<BasicBlock *>>> &DT,
|
2015-07-13 19:21:31 +02:00
|
|
|
Function &F);
|
2014-02-14 23:36:16 +01:00
|
|
|
|
2007-10-08 09:44:39 +02:00
|
|
|
typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
|
|
|
|
|
2012-08-16 17:09:43 +02:00
|
|
|
class BasicBlockEdge {
|
|
|
|
const BasicBlock *Start;
|
|
|
|
const BasicBlock *End;
|
|
|
|
public:
|
|
|
|
BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
|
|
|
|
Start(Start_), End(End_) { }
|
|
|
|
const BasicBlock *getStart() const {
|
|
|
|
return Start;
|
|
|
|
}
|
|
|
|
const BasicBlock *getEnd() const {
|
|
|
|
return End;
|
|
|
|
}
|
|
|
|
bool isSingleEdge() const;
|
|
|
|
};
|
2012-08-07 19:30:46 +02:00
|
|
|
|
2016-07-11 06:37:53 +02:00
|
|
|
template <> struct DenseMapInfo<BasicBlockEdge> {
|
|
|
|
static unsigned getHashValue(const BasicBlockEdge *V);
|
|
|
|
typedef DenseMapInfo<const BasicBlock *> BBInfo;
|
|
|
|
static inline BasicBlockEdge getEmptyKey() {
|
|
|
|
return BasicBlockEdge(BBInfo::getEmptyKey(), BBInfo::getEmptyKey());
|
|
|
|
}
|
|
|
|
static inline BasicBlockEdge getTombstoneKey() {
|
|
|
|
return BasicBlockEdge(BBInfo::getTombstoneKey(), BBInfo::getTombstoneKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getHashValue(const BasicBlockEdge &Edge) {
|
|
|
|
return hash_combine(BBInfo::getHashValue(Edge.getStart()),
|
|
|
|
BBInfo::getHashValue(Edge.getEnd()));
|
|
|
|
}
|
|
|
|
static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS) {
|
|
|
|
return BBInfo::isEqual(LHS.getStart(), RHS.getStart()) &&
|
|
|
|
BBInfo::isEqual(LHS.getEnd(), RHS.getEnd());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-13 14:06:58 +01:00
|
|
|
/// \brief Concrete subclass of DominatorTreeBase that is used to compute a
|
|
|
|
/// normal dominator tree.
|
2015-09-23 20:39:37 +02:00
|
|
|
///
|
|
|
|
/// Definition: A block is said to be forward statically reachable if there is
|
|
|
|
/// a path from the entry of the function to the block. A statically reachable
|
|
|
|
/// block may become statically unreachable during optimization.
|
|
|
|
///
|
|
|
|
/// A forward unreachable block may appear in the dominator tree, or it may
|
|
|
|
/// not. If it does, dominance queries will return results as if all reachable
|
|
|
|
/// blocks dominate it. When asking for a Node corresponding to a potentially
|
|
|
|
/// unreachable block, calling code must handle the case where the block was
|
|
|
|
/// unreachable and the result of getNode() is nullptr.
|
|
|
|
///
|
|
|
|
/// Generally, a block known to be unreachable when the dominator tree is
|
|
|
|
/// constructed will not be in the tree. One which becomes unreachable after
|
|
|
|
/// the dominator tree is initially constructed may still exist in the tree,
|
|
|
|
/// even if the tree is properly updated. Calling code should not rely on the
|
|
|
|
/// preceding statements; this is stated only to assist human understanding.
|
2014-01-13 14:07:17 +01:00
|
|
|
class DominatorTree : public DominatorTreeBase<BasicBlock> {
|
2006-10-03 07:24:56 +02:00
|
|
|
public:
|
2014-01-13 14:07:17 +01:00
|
|
|
typedef DominatorTreeBase<BasicBlock> Base;
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2014-01-13 14:07:17 +01:00
|
|
|
DominatorTree() : DominatorTreeBase<BasicBlock>(false) {}
|
2015-07-17 01:23:35 +02:00
|
|
|
explicit DominatorTree(Function &F) : DominatorTreeBase<BasicBlock>(false) {
|
|
|
|
recalculate(F);
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2014-01-13 14:06:58 +01:00
|
|
|
/// \brief Returns *false* if the other dominator tree matches this dominator
|
|
|
|
/// tree.
|
2014-01-13 14:07:17 +01:00
|
|
|
inline bool compare(const DominatorTree &Other) const {
|
|
|
|
const DomTreeNode *R = getRootNode();
|
|
|
|
const DomTreeNode *OtherR = Other.getRootNode();
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2008-07-01 19:44:24 +02:00
|
|
|
if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
|
|
|
|
return true;
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2014-01-13 14:07:17 +01:00
|
|
|
if (Base::compare(Other))
|
2008-07-01 19:44:24 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-13 14:07:17 +01:00
|
|
|
// Ensure base-class overloads are visible.
|
|
|
|
using Base::dominates;
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2014-01-13 14:07:17 +01:00
|
|
|
/// \brief Return true if Def dominates a use in User.
|
|
|
|
///
|
|
|
|
/// This performs the special checks necessary if Def and User are in the same
|
|
|
|
/// basic block. Note that Def doesn't dominate a use in Def itself!
|
2012-04-13 01:31:46 +02:00
|
|
|
bool dominates(const Instruction *Def, const Use &U) const;
|
2012-02-26 03:19:19 +01:00
|
|
|
bool dominates(const Instruction *Def, const Instruction *User) const;
|
|
|
|
bool dominates(const Instruction *Def, const BasicBlock *BB) const;
|
2012-08-07 19:30:46 +02:00
|
|
|
bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
|
|
|
|
bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
|
2012-01-20 08:41:13 +01:00
|
|
|
|
2014-01-13 14:07:17 +01:00
|
|
|
// Ensure base class overloads are visible.
|
|
|
|
using Base::isReachableFromEntry;
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2014-01-13 14:07:17 +01:00
|
|
|
/// \brief Provide an overload for a Use.
|
2012-04-13 01:31:46 +02:00
|
|
|
bool isReachableFromEntry(const Use &U) const;
|
|
|
|
|
2014-01-13 14:07:17 +01:00
|
|
|
/// \brief Verify the correctness of the domtree by re-computing it.
|
|
|
|
///
|
|
|
|
/// This should only be used for debugging as it aborts the program if the
|
|
|
|
/// verification fails.
|
|
|
|
void verifyDomTree() const;
|
2006-10-03 07:24:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
//===-------------------------------------
|
2014-01-13 14:06:58 +01:00
|
|
|
// DominatorTree GraphTraits specializations so the DominatorTree can be
|
|
|
|
// iterable by generic graph iterators.
|
|
|
|
|
2015-08-07 20:04:17 +02:00
|
|
|
template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
|
2016-08-12 00:36:16 +02:00
|
|
|
typedef Node *NodeRef;
|
2015-08-07 20:04:17 +02:00
|
|
|
typedef ChildIterator ChildIteratorType;
|
2016-10-05 23:36:16 +02:00
|
|
|
typedef df_iterator<Node *, df_iterator_default_set<Node*>> nodes_iterator;
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2016-08-22 23:09:30 +02:00
|
|
|
static NodeRef getEntryNode(NodeRef N) { return N; }
|
2016-08-31 18:48:13 +02:00
|
|
|
static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
|
|
|
|
static ChildIteratorType child_end(NodeRef N) { return N->end(); }
|
2009-10-18 06:05:53 +02:00
|
|
|
|
2016-08-22 23:09:30 +02:00
|
|
|
static nodes_iterator nodes_begin(NodeRef N) {
|
2009-10-18 06:05:53 +02:00
|
|
|
return df_begin(getEntryNode(N));
|
|
|
|
}
|
|
|
|
|
2016-08-22 23:09:30 +02:00
|
|
|
static nodes_iterator nodes_end(NodeRef N) { return df_end(getEntryNode(N)); }
|
2006-10-03 07:24:56 +02:00
|
|
|
};
|
|
|
|
|
2015-08-07 20:04:17 +02:00
|
|
|
template <>
|
|
|
|
struct GraphTraits<DomTreeNode *>
|
|
|
|
: public DomTreeGraphTraitsBase<DomTreeNode, DomTreeNode::iterator> {};
|
2015-07-16 20:23:57 +02:00
|
|
|
|
2015-08-07 20:04:17 +02:00
|
|
|
template <>
|
|
|
|
struct GraphTraits<const DomTreeNode *>
|
|
|
|
: public DomTreeGraphTraitsBase<const DomTreeNode,
|
|
|
|
DomTreeNode::const_iterator> {};
|
2015-07-16 20:23:57 +02:00
|
|
|
|
2006-10-03 07:24:56 +02:00
|
|
|
template <> struct GraphTraits<DominatorTree*>
|
2009-10-18 06:05:53 +02:00
|
|
|
: public GraphTraits<DomTreeNode*> {
|
2016-08-22 23:09:30 +02:00
|
|
|
static NodeRef getEntryNode(DominatorTree *DT) { return DT->getRootNode(); }
|
2009-10-18 06:05:53 +02:00
|
|
|
|
|
|
|
static nodes_iterator nodes_begin(DominatorTree *N) {
|
|
|
|
return df_begin(getEntryNode(N));
|
|
|
|
}
|
|
|
|
|
|
|
|
static nodes_iterator nodes_end(DominatorTree *N) {
|
|
|
|
return df_end(getEntryNode(N));
|
|
|
|
}
|
2006-10-03 07:24:56 +02:00
|
|
|
};
|
|
|
|
|
2014-01-13 14:07:17 +01:00
|
|
|
/// \brief Analysis pass which computes a \c DominatorTree.
|
2016-03-11 11:33:22 +01:00
|
|
|
class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> {
|
|
|
|
friend AnalysisInfoMixin<DominatorTreeAnalysis>;
|
2016-03-11 11:22:49 +01:00
|
|
|
static char PassID;
|
|
|
|
|
|
|
|
public:
|
2015-01-14 11:19:28 +01:00
|
|
|
/// \brief Provide the result typedef for this analysis pass.
|
|
|
|
typedef DominatorTree Result;
|
|
|
|
|
|
|
|
/// \brief Run the analysis pass over a function and produce a dominator tree.
|
2016-08-09 02:28:15 +02:00
|
|
|
DominatorTree run(Function &F, FunctionAnalysisManager &);
|
2015-01-14 11:19:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Printer pass for the \c DominatorTree.
|
2016-03-11 11:33:22 +01:00
|
|
|
class DominatorTreePrinterPass
|
|
|
|
: public PassInfoMixin<DominatorTreePrinterPass> {
|
2015-01-14 11:19:28 +01:00
|
|
|
raw_ostream &OS;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit DominatorTreePrinterPass(raw_ostream &OS);
|
2016-08-09 02:28:15 +02:00
|
|
|
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
|
2015-01-14 11:19:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Verifier pass for the \c DominatorTree.
|
2016-03-11 11:33:22 +01:00
|
|
|
struct DominatorTreeVerifierPass : PassInfoMixin<DominatorTreeVerifierPass> {
|
2016-08-09 02:28:15 +02:00
|
|
|
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
|
2015-01-14 11:19:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Legacy analysis pass which computes a \c DominatorTree.
|
2014-01-13 14:07:17 +01:00
|
|
|
class DominatorTreeWrapperPass : public FunctionPass {
|
|
|
|
DominatorTree DT;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
DominatorTreeWrapperPass() : FunctionPass(ID) {
|
|
|
|
initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
DominatorTree &getDomTree() { return DT; }
|
|
|
|
const DominatorTree &getDomTree() const { return DT; }
|
|
|
|
|
2014-03-05 07:35:38 +01:00
|
|
|
bool runOnFunction(Function &F) override;
|
2014-01-13 14:07:17 +01:00
|
|
|
|
2014-03-05 07:35:38 +01:00
|
|
|
void verifyAnalysis() const override;
|
2014-01-13 14:07:17 +01:00
|
|
|
|
2014-03-05 07:35:38 +01:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2014-01-13 14:07:17 +01:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
2014-03-05 07:35:38 +01:00
|
|
|
void releaseMemory() override { DT.releaseMemory(); }
|
2014-01-13 14:07:17 +01:00
|
|
|
|
2014-04-09 08:08:46 +02:00
|
|
|
void print(raw_ostream &OS, const Module *M = nullptr) const override;
|
2014-01-13 14:07:17 +01:00
|
|
|
};
|
|
|
|
|
2015-06-23 11:49:53 +02:00
|
|
|
} // End llvm namespace
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2001-07-02 07:45:17 +02:00
|
|
|
#endif
|