1 //===- PostOrderCFGView.h - Post order view of CFG blocks -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements post order view of the blocks in a CFG.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_POSTORDERCFGVIEW_H
15 #define LLVM_CLANG_ANALYSIS_ANALYSES_POSTORDERCFGVIEW_H
17 #include "clang/Analysis/AnalysisDeclContext.h"
18 #include "clang/Analysis/CFG.h"
19 #include "clang/Basic/LLVM.h"
20 #include "llvm/ADT/BitVector.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/None.h"
23 #include "llvm/ADT/PostOrderIterator.h"
29 class PostOrderCFGView : public ManagedAnalysis {
30 virtual void anchor();
33 /// Implements a set of CFGBlocks using a BitVector.
35 /// This class contains a minimal interface, primarily dictated by the SetType
36 /// template parameter of the llvm::po_iterator template, as used with
37 /// external storage. We also use this set to keep track of which CFGBlocks we
38 /// visit during the analysis.
40 llvm::BitVector VisitedBlockIDs;
43 // po_iterator requires this iterator, but the only interface needed is the
45 struct iterator { using value_type = const CFGBlock *; };
47 CFGBlockSet() = default;
48 CFGBlockSet(const CFG *G) : VisitedBlockIDs(G->getNumBlockIDs(), false) {}
50 /// Set the bit associated with a particular CFGBlock.
51 /// This is the important method for the SetType template parameter.
52 std::pair<llvm::NoneType, bool> insert(const CFGBlock *Block) {
53 // Note that insert() is called by po_iterator, which doesn't check to
54 // make sure that Block is non-null. Moreover, the CFGBlock iterator will
55 // occasionally hand out null pointers for pruned edges, so we catch those
58 return std::make_pair(None, false); // if an edge is trivially false.
59 if (VisitedBlockIDs.test(Block->getBlockID()))
60 return std::make_pair(None, false);
61 VisitedBlockIDs.set(Block->getBlockID());
62 return std::make_pair(None, true);
65 /// Check if the bit for a CFGBlock has been already set.
66 /// This method is for tracking visited blocks in the main threadsafety
67 /// loop. Block must not be null.
68 bool alreadySet(const CFGBlock *Block) {
69 return VisitedBlockIDs.test(Block->getBlockID());
74 using po_iterator = llvm::po_iterator<const CFG *, CFGBlockSet, true>;
75 std::vector<const CFGBlock *> Blocks;
77 using BlockOrderTy = llvm::DenseMap<const CFGBlock *, unsigned>;
78 BlockOrderTy BlockOrder;
81 friend struct BlockOrderCompare;
83 using iterator = std::vector<const CFGBlock *>::reverse_iterator;
84 using const_iterator = std::vector<const CFGBlock *>::const_reverse_iterator;
86 PostOrderCFGView(const CFG *cfg);
88 iterator begin() { return Blocks.rbegin(); }
89 iterator end() { return Blocks.rend(); }
91 const_iterator begin() const { return Blocks.rbegin(); }
92 const_iterator end() const { return Blocks.rend(); }
94 bool empty() const { return begin() == end(); }
96 struct BlockOrderCompare {
97 const PostOrderCFGView &POV;
100 BlockOrderCompare(const PostOrderCFGView &pov) : POV(pov) {}
102 bool operator()(const CFGBlock *b1, const CFGBlock *b2) const;
105 BlockOrderCompare getComparator() const {
106 return BlockOrderCompare(*this);
109 // Used by AnalyisContext to construct this object.
110 static const void *getTag();
112 static PostOrderCFGView *create(AnalysisDeclContext &analysisContext);
117 #endif // LLVM_CLANG_ANALYSIS_ANALYSES_POSTORDERCFGVIEW_H