Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame^] | 1 | //==- GREngine.cpp - Path-Sensitive Dataflow Engine ----------------*- C++ -*-// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a generic engine for intraprocedural, path-sensitive, |
| 11 | // dataflow analysis via graph reachability engine. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Analysis/PathSensitive/GREngine.h" |
| 16 | #include "clang/AST/Expr.h" |
| 17 | #include "llvm/Support/Compiler.h" |
| 18 | #include "llvm/Support/Casting.h" |
| 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | #include <vector> |
| 21 | |
| 22 | using llvm::cast; |
| 23 | using llvm::isa; |
| 24 | using namespace clang; |
| 25 | |
| 26 | namespace { |
| 27 | class VISIBILITY_HIDDEN DFS : public GRWorkList { |
| 28 | llvm::SmallVector<GRWorkListUnit,20> Stack; |
| 29 | public: |
| 30 | virtual bool hasWork() const { |
| 31 | return !Stack.empty(); |
| 32 | } |
| 33 | |
| 34 | virtual void Enqueue(const GRWorkListUnit& U) { |
| 35 | Stack.push_back(U); |
| 36 | } |
| 37 | |
| 38 | virtual GRWorkListUnit Dequeue() { |
| 39 | assert (!Stack.empty()); |
| 40 | const GRWorkListUnit& U = Stack.back(); |
| 41 | Stack.pop_back(); // This technically "invalidates" U, but we are fine. |
| 42 | return U; |
| 43 | } |
| 44 | }; |
| 45 | } // end anonymous namespace |
| 46 | |
| 47 | GRWorkList* GRWorkList::MakeDFS() { return new DFS(); } |
| 48 | |
| 49 | /// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps. |
| 50 | bool GREngineImpl::ExecuteWorkList(unsigned Steps) { |
| 51 | |
| 52 | if (G->num_roots() == 0) { // Initialize the analysis by constructing |
| 53 | // the root if none exists. |
| 54 | |
| 55 | CFGBlock* Entry = &cfg.getEntry(); |
| 56 | |
| 57 | assert (Entry->empty() && |
| 58 | "Entry block must be empty."); |
| 59 | |
| 60 | assert (Entry->succ_size() == 1 && |
| 61 | "Entry block must have 1 successor."); |
| 62 | |
| 63 | // Get the solitary successor. |
| 64 | CFGBlock* Succ = *(Entry->succ_begin()); |
| 65 | |
| 66 | // Construct an edge representing the |
| 67 | // starting location in the function. |
| 68 | BlockEdge StartLoc(cfg, Entry, Succ); |
| 69 | |
| 70 | // Generate the root. |
| 71 | GenerateNode(StartLoc, getInitialState()); |
| 72 | } |
| 73 | |
| 74 | while (Steps && WList->hasWork()) { |
| 75 | --Steps; |
| 76 | const GRWorkListUnit& WU = WList->Dequeue(); |
| 77 | ExplodedNodeImpl* Node = WU.getNode(); |
| 78 | |
| 79 | // Dispatch on the location type. |
| 80 | switch (Node->getLocation().getKind()) { |
| 81 | default: |
| 82 | assert (isa<BlockEdge>(Node->getLocation())); |
| 83 | HandleBlockEdge(cast<BlockEdge>(Node->getLocation()), Node); |
| 84 | break; |
| 85 | |
| 86 | case ProgramPoint::BlockEntranceKind: |
| 87 | HandleBlockEntrance(cast<BlockEntrance>(Node->getLocation()), Node); |
| 88 | break; |
| 89 | |
| 90 | case ProgramPoint::BlockExitKind: |
| 91 | HandleBlockExit(cast<BlockExit>(Node->getLocation()), Node); |
| 92 | break; |
| 93 | |
| 94 | case ProgramPoint::PostStmtKind: |
| 95 | HandlePostStmt(cast<PostStmt>(Node->getLocation()), WU.getBlock(), |
| 96 | WU.getIndex(), Node); |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return WList->hasWork(); |
| 102 | } |
| 103 | |
| 104 | void GREngineImpl::HandleBlockEdge(const BlockEdge& L, ExplodedNodeImpl* Pred) { |
| 105 | |
| 106 | CFGBlock* Blk = L.getDst(); |
| 107 | |
| 108 | // Check if we are entering the EXIT block. |
| 109 | if (Blk == &cfg.getExit()) { |
| 110 | |
| 111 | assert (cfg.getExit().size() == 0 && "EXIT block cannot contain Stmts."); |
| 112 | |
| 113 | // Process the final state transition. |
| 114 | void* State = ProcessEOP(Blk, Pred->State); |
| 115 | |
| 116 | bool IsNew; |
| 117 | ExplodedNodeImpl* Node = G->getNodeImpl(BlockEntrance(Blk), State, &IsNew); |
| 118 | Node->addPredecessor(Pred); |
| 119 | |
| 120 | // If the node was freshly created, mark it as an "End-Of-Path" node. |
| 121 | if (IsNew) G->addEndOfPath(Node); |
| 122 | |
| 123 | // This path is done. Don't enqueue any more nodes. |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | // FIXME: we will dispatch to a function that |
| 128 | // manipulates the state at the entrance to a block. |
| 129 | |
| 130 | if (!Blk->empty()) |
| 131 | GenerateNode(BlockEntrance(Blk), Pred->State, Pred); |
| 132 | else |
| 133 | GenerateNode(BlockExit(Blk), Pred->State, Pred); |
| 134 | } |
| 135 | |
| 136 | void GREngineImpl::HandleBlockEntrance(const BlockEntrance& L, |
| 137 | ExplodedNodeImpl* Pred) { |
| 138 | |
| 139 | if (Stmt* S = L.getFirstStmt()) { |
| 140 | GRNodeBuilderImpl Builder(L.getBlock(), 0, Pred, this); |
| 141 | ProcessStmt(S, Builder); |
| 142 | } |
| 143 | else |
| 144 | GenerateNode(BlockExit(L.getBlock()), Pred->State, Pred); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | void GREngineImpl::HandleBlockExit(const BlockExit& L, ExplodedNodeImpl* Pred) { |
| 149 | |
| 150 | CFGBlock* B = L.getBlock(); |
| 151 | |
| 152 | if (Stmt* Terminator = B->getTerminator()) |
| 153 | ProcessTerminator(Terminator, B, Pred); |
| 154 | else { |
| 155 | assert (B->succ_size() == 1 && |
| 156 | "Blocks with no terminator should have at most 1 successor."); |
| 157 | |
| 158 | GenerateNode(BlockEdge(cfg,B,*(B->succ_begin())), Pred->State, Pred); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void GREngineImpl::HandlePostStmt(const PostStmt& L, CFGBlock* B, |
| 163 | unsigned StmtIdx, ExplodedNodeImpl* Pred) { |
| 164 | |
| 165 | assert (!B->empty()); |
| 166 | |
| 167 | if (StmtIdx == B->size()) { |
| 168 | // FIXME: This is essentially an epsilon-transition. Do we need it? |
| 169 | // It does simplify the logic, and it is also another point |
| 170 | // were we could introduce a dispatch to the client. |
| 171 | GenerateNode(BlockExit(B), Pred->State, Pred); |
| 172 | } |
| 173 | else { |
| 174 | GRNodeBuilderImpl Builder(B, StmtIdx, Pred, this); |
| 175 | ProcessStmt(L.getStmt(), Builder); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | typedef llvm::DenseMap<Stmt*,Stmt*> ParentMapTy; |
| 180 | /// PopulateParentMap - Recurse the AST starting at 'Parent' and add the |
| 181 | /// mappings between child and parent to ParentMap. |
| 182 | static void PopulateParentMap(Stmt* Parent, ParentMapTy& M) { |
| 183 | for (Stmt::child_iterator I=Parent->child_begin(), |
| 184 | E=Parent->child_end(); I!=E; ++I) { |
| 185 | |
| 186 | assert (M.find(*I) == M.end()); |
| 187 | M[*I] = Parent; |
| 188 | PopulateParentMap(*I, M); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /// GenerateNode - Utility method to generate nodes, hook up successors, |
| 193 | /// and add nodes to the worklist. |
| 194 | void GREngineImpl::GenerateNode(const ProgramPoint& Loc, void* State, |
| 195 | ExplodedNodeImpl* Pred) { |
| 196 | |
| 197 | bool IsNew; |
| 198 | ExplodedNodeImpl* Node = G->getNodeImpl(Loc, State, &IsNew); |
| 199 | |
| 200 | if (Pred) |
| 201 | Node->addPredecessor(Pred); // Link 'Node' with its predecessor. |
| 202 | else { |
| 203 | assert (IsNew); |
| 204 | G->addRoot(Node); // 'Node' has no predecessor. Make it a root. |
| 205 | } |
| 206 | |
| 207 | // Only add 'Node' to the worklist if it was freshly generated. |
| 208 | if (IsNew) WList->Enqueue(GRWorkListUnit(Node)); |
| 209 | } |
| 210 | |
| 211 | GRNodeBuilderImpl::GRNodeBuilderImpl(CFGBlock* b, unsigned idx, |
| 212 | ExplodedNodeImpl* N, GREngineImpl* e) |
| 213 | : Eng(*e), B(*b), Idx(idx), LastNode(N), Populated(false) { |
| 214 | Deferred.insert(N); |
| 215 | } |
| 216 | |
| 217 | GRNodeBuilderImpl::~GRNodeBuilderImpl() { |
| 218 | for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I) |
| 219 | if (!(*I)->isInfeasible()) |
| 220 | GenerateAutoTransition(*I); |
| 221 | } |
| 222 | |
| 223 | void GRNodeBuilderImpl::GenerateAutoTransition(ExplodedNodeImpl* N) { |
| 224 | assert (!N->isInfeasible()); |
| 225 | |
| 226 | PostStmt Loc(getStmt()); |
| 227 | |
| 228 | if (Loc == N->getLocation()) { |
| 229 | // Note: 'N' should be a fresh node because otherwise it shouldn't be |
| 230 | // a member of Deferred. |
| 231 | Eng.WList->Enqueue(N, B, Idx+1); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | bool IsNew; |
| 236 | ExplodedNodeImpl* Succ = Eng.G->getNodeImpl(Loc, N->State, &IsNew); |
| 237 | Succ->addPredecessor(N); |
| 238 | |
| 239 | if (IsNew) |
| 240 | Eng.WList->Enqueue(Succ, B, Idx+1); |
| 241 | } |
| 242 | |
| 243 | ExplodedNodeImpl* GRNodeBuilderImpl::generateNodeImpl(Stmt* S, void* State, |
| 244 | ExplodedNodeImpl* Pred) { |
| 245 | |
| 246 | bool IsNew; |
| 247 | ExplodedNodeImpl* N = Eng.G->getNodeImpl(PostStmt(S), State, &IsNew); |
| 248 | N->addPredecessor(Pred); |
| 249 | Deferred.erase(Pred); |
| 250 | |
| 251 | HasGeneratedNode = true; |
| 252 | |
| 253 | if (IsNew) { |
| 254 | Deferred.insert(N); |
| 255 | LastNode = N; |
| 256 | return N; |
| 257 | } |
| 258 | |
| 259 | LastNode = NULL; |
| 260 | return NULL; |
| 261 | } |