| Eugene Zelenko | bbe2531 | 2018-03-16 21:22:42 +0000 | [diff] [blame] | 1 | //===- CFGReachabilityAnalysis.cpp - Basic reachability analysis ----------===// |
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 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 flow-sensitive, (mostly) path-insensitive reachability |
| 11 | // analysis based on Clang's CFGs. Clients can query if a given basic block |
| 12 | // is reachable within the CFG. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h" |
| 17 | #include "clang/Analysis/CFG.h" |
| Eugene Zelenko | bbe2531 | 2018-03-16 21:22:42 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/BitVector.h" |
| 19 | #include "llvm/ADT/SmallVector.h" |
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | |
| Eugene Zelenko | bbe2531 | 2018-03-16 21:22:42 +0000 | [diff] [blame] | 23 | CFGReverseBlockReachabilityAnalysis::CFGReverseBlockReachabilityAnalysis( |
| 24 | const CFG &cfg) |
| 25 | : analyzed(cfg.getNumBlockIDs(), false) {} |
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 26 | |
| Ted Kremenek | ddc06d0 | 2011-03-19 01:00:33 +0000 | [diff] [blame] | 27 | bool CFGReverseBlockReachabilityAnalysis::isReachable(const CFGBlock *Src, |
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 28 | const CFGBlock *Dst) { |
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 29 | const unsigned DstBlockID = Dst->getBlockID(); |
| Fangrui Song | 32fa871 | 2018-07-28 00:48:05 +0000 | [diff] [blame] | 30 | |
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 31 | // If we haven't analyzed the destination node, run the analysis now |
| 32 | if (!analyzed[DstBlockID]) { |
| 33 | mapReachability(Dst); |
| 34 | analyzed[DstBlockID] = true; |
| 35 | } |
| Fangrui Song | 32fa871 | 2018-07-28 00:48:05 +0000 | [diff] [blame] | 36 | |
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 37 | // Return the cached result |
| 38 | return reachable[DstBlockID][Src->getBlockID()]; |
| 39 | } |
| 40 | |
| 41 | // Maps reachability to a common node by walking the predecessors of the |
| 42 | // destination node. |
| Ted Kremenek | ddc06d0 | 2011-03-19 01:00:33 +0000 | [diff] [blame] | 43 | void CFGReverseBlockReachabilityAnalysis::mapReachability(const CFGBlock *Dst) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 44 | SmallVector<const CFGBlock *, 11> worklist; |
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 45 | llvm::BitVector visited(analyzed.size()); |
| Fangrui Song | 32fa871 | 2018-07-28 00:48:05 +0000 | [diff] [blame] | 46 | |
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 47 | ReachableSet &DstReachability = reachable[Dst->getBlockID()]; |
| 48 | DstReachability.resize(analyzed.size(), false); |
| Fangrui Song | 32fa871 | 2018-07-28 00:48:05 +0000 | [diff] [blame] | 49 | |
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 50 | // Start searching from the destination node, since we commonly will perform |
| 51 | // multiple queries relating to a destination node. |
| 52 | worklist.push_back(Dst); |
| 53 | bool firstRun = true; |
| Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 54 | |
| 55 | while (!worklist.empty()) { |
| 56 | const CFGBlock *block = worklist.pop_back_val(); |
| 57 | |
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 58 | if (visited[block->getBlockID()]) |
| 59 | continue; |
| 60 | visited[block->getBlockID()] = true; |
| Fangrui Song | 32fa871 | 2018-07-28 00:48:05 +0000 | [diff] [blame] | 61 | |
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 62 | // Update reachability information for this node -> Dst |
| 63 | if (!firstRun) { |
| 64 | // Don't insert Dst -> Dst unless it was a predecessor of itself |
| 65 | DstReachability[block->getBlockID()] = true; |
| 66 | } |
| 67 | else |
| 68 | firstRun = false; |
| Fangrui Song | 32fa871 | 2018-07-28 00:48:05 +0000 | [diff] [blame] | 69 | |
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 70 | // Add the predecessors to the worklist. |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 71 | for (CFGBlock::const_pred_iterator i = block->pred_begin(), |
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 72 | e = block->pred_end(); i != e; ++i) { |
| Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 73 | if (*i) |
| 74 | worklist.push_back(*i); |
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | } |