2014-01-13 09:26:24 +00:00
|
|
|
//===- Dominators.h - Dominator Info Calculation ----------------*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-02 05:45:17 +00:00
|
|
|
//
|
2011-01-02 22:09:33 +00:00
|
|
|
// This file defines the DominatorTree class, which provides fast and efficient
|
|
|
|
// dominance queries.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2001-07-02 05:45:17 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-01-13 09:26:24 +00:00
|
|
|
#ifndef LLVM_IR_DOMINATORS_H
|
|
|
|
#define LLVM_IR_DOMINATORS_H
|
2001-07-02 05:45:17 +00:00
|
|
|
|
2007-10-16 19:59:25 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2009-10-18 04:05:53 +00:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2007-10-23 21:04:37 +00:00
|
|
|
#include "llvm/ADT/GraphTraits.h"
|
2007-10-16 19:59:25 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2014-01-13 10:52:56 +00:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2014-03-04 11:45:46 +00:00
|
|
|
#include "llvm/IR/CFG.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
2012-12-03 17:02:12 +00:00
|
|
|
#include "llvm/Pass.h"
|
2007-10-16 19:59:25 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2014-03-04 10:07:28 +00:00
|
|
|
#include "llvm/Support/GenericDomTree.h"
|
2009-08-23 05:17:37 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-10-08 07:44:39 +00:00
|
|
|
#include <algorithm>
|
2003-06-30 21:59:07 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2015-01-14 10:19:28 +00:00
|
|
|
// FIXME: Replace this brittle forward declaration with the include of the new
|
|
|
|
// PassManager.h when doing so doesn't break the PassManagerBuilder.
|
|
|
|
template <typename IRUnitT> class AnalysisManager;
|
|
|
|
class PreservedAnalyses;
|
|
|
|
|
2007-10-16 19:59:25 +00:00
|
|
|
EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>);
|
2014-01-13 10:52:56 +00:00
|
|
|
EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<BasicBlock>);
|
2007-10-16 19:59:25 +00:00
|
|
|
|
2014-02-14 22:36:16 +00:00
|
|
|
#define LLVM_COMMA ,
|
2014-02-14 22:48:49 +00:00
|
|
|
EXTERN_TEMPLATE_INSTANTIATION(void Calculate<Function LLVM_COMMA BasicBlock *>(
|
2014-02-15 00:34:43 +00:00
|
|
|
DominatorTreeBase<GraphTraits<BasicBlock *>::NodeType> &DT LLVM_COMMA
|
|
|
|
Function &F));
|
|
|
|
EXTERN_TEMPLATE_INSTANTIATION(
|
|
|
|
void Calculate<Function LLVM_COMMA Inverse<BasicBlock *> >(
|
|
|
|
DominatorTreeBase<GraphTraits<Inverse<BasicBlock *> >::NodeType> &DT
|
|
|
|
LLVM_COMMA Function &F));
|
2014-02-14 22:36:16 +00:00
|
|
|
#undef LLVM_COMMA
|
|
|
|
|
2007-10-08 07:44:39 +00:00
|
|
|
typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
|
|
|
|
|
2012-08-16 15:09:43 +00: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 17:30:46 +00:00
|
|
|
|
2014-01-13 13:06:58 +00:00
|
|
|
/// \brief Concrete subclass of DominatorTreeBase that is used to compute a
|
|
|
|
/// normal dominator tree.
|
2014-01-13 13:07:17 +00:00
|
|
|
class DominatorTree : public DominatorTreeBase<BasicBlock> {
|
2006-10-03 05:24:56 +00:00
|
|
|
public:
|
2014-01-13 13:07:17 +00:00
|
|
|
typedef DominatorTreeBase<BasicBlock> Base;
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2014-01-13 13:07:17 +00:00
|
|
|
DominatorTree() : DominatorTreeBase<BasicBlock>(false) {}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2015-01-14 10:07:19 +00:00
|
|
|
DominatorTree(DominatorTree &&Arg)
|
|
|
|
: Base(std::move(static_cast<Base &>(Arg))) {}
|
|
|
|
DominatorTree &operator=(DominatorTree &&RHS) {
|
|
|
|
Base::operator=(std::move(static_cast<Base &>(RHS)));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-01-13 13:06:58 +00:00
|
|
|
/// \brief Returns *false* if the other dominator tree matches this dominator
|
|
|
|
/// tree.
|
2014-01-13 13:07:17 +00:00
|
|
|
inline bool compare(const DominatorTree &Other) const {
|
|
|
|
const DomTreeNode *R = getRootNode();
|
|
|
|
const DomTreeNode *OtherR = Other.getRootNode();
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2008-07-01 17:44:24 +00:00
|
|
|
if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
|
|
|
|
return true;
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2014-01-13 13:07:17 +00:00
|
|
|
if (Base::compare(Other))
|
2008-07-01 17:44:24 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-13 13:07:17 +00:00
|
|
|
// Ensure base-class overloads are visible.
|
|
|
|
using Base::dominates;
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2014-01-13 13:07:17 +00: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-12 23:31:46 +00:00
|
|
|
bool dominates(const Instruction *Def, const Use &U) const;
|
2012-02-26 02:19:19 +00:00
|
|
|
bool dominates(const Instruction *Def, const Instruction *User) const;
|
|
|
|
bool dominates(const Instruction *Def, const BasicBlock *BB) const;
|
2012-08-07 17:30:46 +00:00
|
|
|
bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
|
|
|
|
bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
|
2012-01-20 07:41:13 +00:00
|
|
|
|
2014-01-13 13:07:17 +00:00
|
|
|
// Ensure base class overloads are visible.
|
|
|
|
using Base::isReachableFromEntry;
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2014-01-13 13:07:17 +00:00
|
|
|
/// \brief Provide an overload for a Use.
|
2012-04-12 23:31:46 +00:00
|
|
|
bool isReachableFromEntry(const Use &U) const;
|
|
|
|
|
2014-01-13 13:07:17 +00: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 05:24:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//===-------------------------------------
|
2014-01-13 13:06:58 +00:00
|
|
|
// DominatorTree GraphTraits specializations so the DominatorTree can be
|
|
|
|
// iterable by generic graph iterators.
|
|
|
|
|
2009-10-18 04:05:53 +00:00
|
|
|
template <> struct GraphTraits<DomTreeNode*> {
|
2007-06-04 00:32:22 +00:00
|
|
|
typedef DomTreeNode NodeType;
|
2006-10-03 05:24:56 +00:00
|
|
|
typedef NodeType::iterator ChildIteratorType;
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2006-10-03 05:24:56 +00:00
|
|
|
static NodeType *getEntryNode(NodeType *N) {
|
|
|
|
return N;
|
|
|
|
}
|
2009-10-18 04:05:53 +00:00
|
|
|
static inline ChildIteratorType child_begin(NodeType *N) {
|
2006-10-03 05:24:56 +00:00
|
|
|
return N->begin();
|
|
|
|
}
|
2009-10-18 04:05:53 +00:00
|
|
|
static inline ChildIteratorType child_end(NodeType *N) {
|
2006-10-03 05:24:56 +00:00
|
|
|
return N->end();
|
|
|
|
}
|
2009-10-18 04:05:53 +00:00
|
|
|
|
|
|
|
typedef df_iterator<DomTreeNode*> nodes_iterator;
|
|
|
|
|
|
|
|
static nodes_iterator nodes_begin(DomTreeNode *N) {
|
|
|
|
return df_begin(getEntryNode(N));
|
|
|
|
}
|
|
|
|
|
|
|
|
static nodes_iterator nodes_end(DomTreeNode *N) {
|
|
|
|
return df_end(getEntryNode(N));
|
|
|
|
}
|
2006-10-03 05:24:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct GraphTraits<DominatorTree*>
|
2009-10-18 04:05:53 +00:00
|
|
|
: public GraphTraits<DomTreeNode*> {
|
2006-10-03 05:24:56 +00:00
|
|
|
static NodeType *getEntryNode(DominatorTree *DT) {
|
|
|
|
return DT->getRootNode();
|
|
|
|
}
|
2009-10-18 04:05:53 +00: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 05:24:56 +00:00
|
|
|
};
|
|
|
|
|
2014-01-13 13:07:17 +00:00
|
|
|
/// \brief Analysis pass which computes a \c DominatorTree.
|
2015-01-14 10:19:28 +00:00
|
|
|
class DominatorTreeAnalysis {
|
|
|
|
public:
|
|
|
|
/// \brief Provide the result typedef for this analysis pass.
|
|
|
|
typedef DominatorTree Result;
|
|
|
|
|
|
|
|
/// \brief Opaque, unique identifier for this analysis pass.
|
|
|
|
static void *ID() { return (void *)&PassID; }
|
|
|
|
|
|
|
|
/// \brief Run the analysis pass over a function and produce a dominator tree.
|
|
|
|
DominatorTree run(Function &F);
|
|
|
|
|
|
|
|
/// \brief Provide access to a name for this pass for debugging purposes.
|
|
|
|
static StringRef name() { return "DominatorTreeAnalysis"; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
static char PassID;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Printer pass for the \c DominatorTree.
|
|
|
|
class DominatorTreePrinterPass {
|
|
|
|
raw_ostream &OS;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit DominatorTreePrinterPass(raw_ostream &OS);
|
|
|
|
PreservedAnalyses run(Function &F, AnalysisManager<Function> *AM);
|
|
|
|
|
|
|
|
static StringRef name() { return "DominatorTreePrinterPass"; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Verifier pass for the \c DominatorTree.
|
|
|
|
struct DominatorTreeVerifierPass {
|
|
|
|
PreservedAnalyses run(Function &F, AnalysisManager<Function> *AM);
|
|
|
|
|
|
|
|
static StringRef name() { return "DominatorTreeVerifierPass"; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Legacy analysis pass which computes a \c DominatorTree.
|
2014-01-13 13:07:17 +00: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 06:35:38 +00:00
|
|
|
bool runOnFunction(Function &F) override;
|
2014-01-13 13:07:17 +00:00
|
|
|
|
2014-03-05 06:35:38 +00:00
|
|
|
void verifyAnalysis() const override;
|
2014-01-13 13:07:17 +00:00
|
|
|
|
2014-03-05 06:35:38 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2014-01-13 13:07:17 +00:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
2014-03-05 06:35:38 +00:00
|
|
|
void releaseMemory() override { DT.releaseMemory(); }
|
2014-01-13 13:07:17 +00:00
|
|
|
|
2014-04-09 06:08:46 +00:00
|
|
|
void print(raw_ostream &OS, const Module *M = nullptr) const override;
|
2014-01-13 13:07:17 +00:00
|
|
|
};
|
|
|
|
|
2015-06-23 09:49:53 +00:00
|
|
|
} // End llvm namespace
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2001-07-02 05:45:17 +00:00
|
|
|
#endif
|