blob: cdad5b57aee8aa53b91b3a2352647e49c5dcf8be [file] [log] [blame]
Eugene Zelenkobbe25312018-03-16 21:22:42 +00001//===- CFGReachabilityAnalysis.cpp - Basic reachability analysis ----------===//
Ted Kremenek80861ca2011-02-23 01:51:59 +00002//
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 Kremenek80861ca2011-02-23 01:51:59 +000016#include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
17#include "clang/Analysis/CFG.h"
Eugene Zelenkobbe25312018-03-16 21:22:42 +000018#include "llvm/ADT/BitVector.h"
19#include "llvm/ADT/SmallVector.h"
Ted Kremenek80861ca2011-02-23 01:51:59 +000020
21using namespace clang;
22
Eugene Zelenkobbe25312018-03-16 21:22:42 +000023CFGReverseBlockReachabilityAnalysis::CFGReverseBlockReachabilityAnalysis(
24 const CFG &cfg)
25 : analyzed(cfg.getNumBlockIDs(), false) {}
Ted Kremenek80861ca2011-02-23 01:51:59 +000026
Ted Kremenekddc06d02011-03-19 01:00:33 +000027bool CFGReverseBlockReachabilityAnalysis::isReachable(const CFGBlock *Src,
Ted Kremenek80861ca2011-02-23 01:51:59 +000028 const CFGBlock *Dst) {
Ted Kremenek80861ca2011-02-23 01:51:59 +000029 const unsigned DstBlockID = Dst->getBlockID();
Fangrui Song32fa8712018-07-28 00:48:05 +000030
Ted Kremenek80861ca2011-02-23 01:51:59 +000031 // 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 Song32fa8712018-07-28 00:48:05 +000036
Ted Kremenek80861ca2011-02-23 01:51:59 +000037 // 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 Kremenekddc06d02011-03-19 01:00:33 +000043void CFGReverseBlockReachabilityAnalysis::mapReachability(const CFGBlock *Dst) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000044 SmallVector<const CFGBlock *, 11> worklist;
Ted Kremenek80861ca2011-02-23 01:51:59 +000045 llvm::BitVector visited(analyzed.size());
Fangrui Song32fa8712018-07-28 00:48:05 +000046
Ted Kremenek80861ca2011-02-23 01:51:59 +000047 ReachableSet &DstReachability = reachable[Dst->getBlockID()];
48 DstReachability.resize(analyzed.size(), false);
Fangrui Song32fa8712018-07-28 00:48:05 +000049
Ted Kremenek80861ca2011-02-23 01:51:59 +000050 // 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 Wilhelm25284cc2013-08-23 16:11:15 +000054
55 while (!worklist.empty()) {
56 const CFGBlock *block = worklist.pop_back_val();
57
Ted Kremenek80861ca2011-02-23 01:51:59 +000058 if (visited[block->getBlockID()])
59 continue;
60 visited[block->getBlockID()] = true;
Fangrui Song32fa8712018-07-28 00:48:05 +000061
Ted Kremenek80861ca2011-02-23 01:51:59 +000062 // 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 Song32fa8712018-07-28 00:48:05 +000069
Ted Kremenek80861ca2011-02-23 01:51:59 +000070 // Add the predecessors to the worklist.
Fangrui Song6907ce22018-07-30 19:24:48 +000071 for (CFGBlock::const_pred_iterator i = block->pred_begin(),
Ted Kremenek80861ca2011-02-23 01:51:59 +000072 e = block->pred_end(); i != e; ++i) {
Ted Kremenek4b6fee62014-02-27 00:24:00 +000073 if (*i)
74 worklist.push_back(*i);
Ted Kremenek80861ca2011-02-23 01:51:59 +000075 }
76 }
77}