2018-03-17 05:22:42 +08:00
|
|
|
//===- CFGReachabilityAnalysis.cpp - Basic reachability analysis ----------===//
|
2011-02-23 09:51:59 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-02-23 09:51:59 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines a flow-sensitive, (mostly) path-insensitive reachability
|
|
|
|
// analysis based on Clang's CFGs. Clients can query if a given basic block
|
|
|
|
// is reachable within the CFG.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
|
|
|
|
#include "clang/Analysis/CFG.h"
|
2018-03-17 05:22:42 +08:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2011-02-23 09:51:59 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
CFGReverseBlockReachabilityAnalysis::CFGReverseBlockReachabilityAnalysis(
|
|
|
|
const CFG &cfg)
|
|
|
|
: analyzed(cfg.getNumBlockIDs(), false) {}
|
2011-02-23 09:51:59 +08:00
|
|
|
|
2011-03-19 09:00:33 +08:00
|
|
|
bool CFGReverseBlockReachabilityAnalysis::isReachable(const CFGBlock *Src,
|
2011-02-23 09:51:59 +08:00
|
|
|
const CFGBlock *Dst) {
|
|
|
|
const unsigned DstBlockID = Dst->getBlockID();
|
2018-07-28 08:48:05 +08:00
|
|
|
|
2011-02-23 09:51:59 +08:00
|
|
|
// If we haven't analyzed the destination node, run the analysis now
|
|
|
|
if (!analyzed[DstBlockID]) {
|
|
|
|
mapReachability(Dst);
|
|
|
|
analyzed[DstBlockID] = true;
|
|
|
|
}
|
2018-07-28 08:48:05 +08:00
|
|
|
|
2011-02-23 09:51:59 +08:00
|
|
|
// Return the cached result
|
|
|
|
return reachable[DstBlockID][Src->getBlockID()];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maps reachability to a common node by walking the predecessors of the
|
|
|
|
// destination node.
|
2011-03-19 09:00:33 +08:00
|
|
|
void CFGReverseBlockReachabilityAnalysis::mapReachability(const CFGBlock *Dst) {
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<const CFGBlock *, 11> worklist;
|
2011-02-23 09:51:59 +08:00
|
|
|
llvm::BitVector visited(analyzed.size());
|
2018-07-28 08:48:05 +08:00
|
|
|
|
2011-02-23 09:51:59 +08:00
|
|
|
ReachableSet &DstReachability = reachable[Dst->getBlockID()];
|
|
|
|
DstReachability.resize(analyzed.size(), false);
|
2018-07-28 08:48:05 +08:00
|
|
|
|
2011-02-23 09:51:59 +08:00
|
|
|
// Start searching from the destination node, since we commonly will perform
|
|
|
|
// multiple queries relating to a destination node.
|
|
|
|
worklist.push_back(Dst);
|
|
|
|
bool firstRun = true;
|
2013-08-24 00:11:15 +08:00
|
|
|
|
|
|
|
while (!worklist.empty()) {
|
|
|
|
const CFGBlock *block = worklist.pop_back_val();
|
|
|
|
|
2011-02-23 09:51:59 +08:00
|
|
|
if (visited[block->getBlockID()])
|
|
|
|
continue;
|
|
|
|
visited[block->getBlockID()] = true;
|
2018-07-28 08:48:05 +08:00
|
|
|
|
2011-02-23 09:51:59 +08:00
|
|
|
// Update reachability information for this node -> Dst
|
|
|
|
if (!firstRun) {
|
|
|
|
// Don't insert Dst -> Dst unless it was a predecessor of itself
|
|
|
|
DstReachability[block->getBlockID()] = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
firstRun = false;
|
2018-07-28 08:48:05 +08:00
|
|
|
|
2011-02-23 09:51:59 +08:00
|
|
|
// Add the predecessors to the worklist.
|
2018-07-31 03:24:48 +08:00
|
|
|
for (CFGBlock::const_pred_iterator i = block->pred_begin(),
|
2011-02-23 09:51:59 +08:00
|
|
|
e = block->pred_end(); i != e; ++i) {
|
2014-02-27 08:24:00 +08:00
|
|
|
if (*i)
|
|
|
|
worklist.push_back(*i);
|
2011-02-23 09:51:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|