Use 'BitVector' instead of SmallPtrSet<CFGBlock*> in IdempotentOperationsChecker. No real functionality change.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@125494 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Ted Kremenek 2011-02-14 17:59:20 +00:00
Родитель 840c083f79
Коммит 8e3767711f
1 изменённых файлов: 68 добавлений и 53 удалений

Просмотреть файл

@ -54,6 +54,7 @@
#include "clang/AST/Stmt.h" #include "clang/AST/Stmt.h"
#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallSet.h" #include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorHandling.h"
#include <deque> #include <deque>
@ -81,7 +82,7 @@ private:
static bool isUnused(const Expr *E, AnalysisContext *AC); static bool isUnused(const Expr *E, AnalysisContext *AC);
static bool isTruncationExtensionAssignment(const Expr *LHS, static bool isTruncationExtensionAssignment(const Expr *LHS,
const Expr *RHS); const Expr *RHS);
bool PathWasCompletelyAnalyzed(const CFG *C, bool pathWasCompletelyAnalyzed(const CFG *cfg,
const CFGBlock *CB, const CFGBlock *CB,
const CFGStmtMap *CBM, const CFGStmtMap *CBM,
const CoreEngine &CE); const CoreEngine &CE);
@ -90,8 +91,9 @@ private:
static bool isConstantOrPseudoConstant(const DeclRefExpr *DR, static bool isConstantOrPseudoConstant(const DeclRefExpr *DR,
AnalysisContext *AC); AnalysisContext *AC);
static bool containsNonLocalVarDecl(const Stmt *S); static bool containsNonLocalVarDecl(const Stmt *S);
const ExplodedNodeSet getLastRelevantNodes(const CFGBlock *Begin, void getLastRelevantNodes(const CFGBlock *Begin,
const ExplodedNode *N); const ExplodedNode *N,
ExplodedNodeSet &result);
// Hash table and related data structures // Hash table and related data structures
struct BinaryOperatorData { struct BinaryOperatorData {
@ -112,16 +114,19 @@ private:
// from the destination node and cache the results to prevent work // from the destination node and cache the results to prevent work
// duplication. // duplication.
class CFGReachabilityAnalysis { class CFGReachabilityAnalysis {
typedef llvm::SmallSet<unsigned, 32> ReachableSet; typedef llvm::BitVector ReachableSet;
typedef llvm::DenseMap<unsigned, ReachableSet> ReachableMap; typedef llvm::DenseMap<unsigned, ReachableSet> ReachableMap;
ReachableSet analyzed; ReachableSet analyzed;
ReachableMap reachable; ReachableMap reachable;
public: public:
CFGReachabilityAnalysis(const CFG &cfg)
: analyzed(cfg.getNumBlockIDs(), false) {}
inline bool isReachable(const CFGBlock *Src, const CFGBlock *Dst); inline bool isReachable(const CFGBlock *Src, const CFGBlock *Dst);
private: private:
void MapReachability(const CFGBlock *Dst); void MapReachability(const CFGBlock *Dst);
}; };
CFGReachabilityAnalysis CRA; llvm::OwningPtr<CFGReachabilityAnalysis> CRA;
}; };
} }
@ -394,7 +399,7 @@ void IdempotentOperationChecker::VisitEndAnalysis(ExplodedGraph &G,
&AC->getParentMap()); &AC->getParentMap());
// If we can trace back // If we can trace back
if (!PathWasCompletelyAnalyzed(AC->getCFG(), if (!pathWasCompletelyAnalyzed(AC->getCFG(),
CBM->getBlock(B), CBM, CBM->getBlock(B), CBM,
Eng.getCoreEngine())) Eng.getCoreEngine()))
continue; continue;
@ -556,11 +561,14 @@ bool IdempotentOperationChecker::isTruncationExtensionAssignment(
// Returns false if a path to this block was not completely analyzed, or true // Returns false if a path to this block was not completely analyzed, or true
// otherwise. // otherwise.
bool IdempotentOperationChecker::PathWasCompletelyAnalyzed( bool
const CFG *C, IdempotentOperationChecker::pathWasCompletelyAnalyzed(const CFG *cfg,
const CFGBlock *CB, const CFGBlock *CB,
const CFGStmtMap *CBM, const CFGStmtMap *CBM,
const CoreEngine &CE) { const CoreEngine &CE) {
CRA.reset(new CFGReachabilityAnalysis(*cfg));
// Test for reachability from any aborted blocks to this block // Test for reachability from any aborted blocks to this block
typedef CoreEngine::BlocksAborted::const_iterator AbortedIterator; typedef CoreEngine::BlocksAborted::const_iterator AbortedIterator;
for (AbortedIterator I = CE.blocks_aborted_begin(), for (AbortedIterator I = CE.blocks_aborted_begin(),
@ -570,7 +578,7 @@ bool IdempotentOperationChecker::PathWasCompletelyAnalyzed(
// The destination block on the BlockEdge is the first block that was not // The destination block on the BlockEdge is the first block that was not
// analyzed. If we can reach this block from the aborted block, then this // analyzed. If we can reach this block from the aborted block, then this
// block was not completely analyzed. // block was not completely analyzed.
if (CRA.isReachable(BE.getDst(), CB)) if (CRA->isReachable(BE.getDst(), CB))
return false; return false;
} }
@ -605,14 +613,14 @@ bool IdempotentOperationChecker::PathWasCompletelyAnalyzed(
return CRA.isReachable(B, TargetBlock); return CRA.isReachable(B, TargetBlock);
} }
}; };
VisitWL visitWL(CBM, CB, CRA); VisitWL visitWL(CBM, CB, *CRA.get());
// Were there any items in the worklist that could potentially reach // Were there any items in the worklist that could potentially reach
// this block? // this block?
if (CE.getWorkList()->visitItemsInWorkList(visitWL)) if (CE.getWorkList()->visitItemsInWorkList(visitWL))
return false; return false;
// Verify that this block is reachable from the entry block // Verify that this block is reachable from the entry block
if (!CRA.isReachable(&C->getEntry(), CB)) if (!CRA->isReachable(&cfg->getEntry(), CB))
return false; return false;
// If we get to this point, there is no connection to the entry block or an // If we get to this point, there is no connection to the entry block or an
@ -754,41 +762,42 @@ bool IdempotentOperationChecker::containsNonLocalVarDecl(const Stmt *S) {
// subsequent execution could not affect the idempotent operation on this path. // subsequent execution could not affect the idempotent operation on this path.
// This is useful for displaying paths after the point of the error, providing // This is useful for displaying paths after the point of the error, providing
// an example of how this idempotent operation cannot change. // an example of how this idempotent operation cannot change.
const ExplodedNodeSet IdempotentOperationChecker::getLastRelevantNodes( void IdempotentOperationChecker::getLastRelevantNodes(
const CFGBlock *Begin, const ExplodedNode *N) { const CFGBlock *Begin, const ExplodedNode *node,
std::deque<const ExplodedNode *> WorkList; ExplodedNodeSet &result) {
llvm::SmallPtrSet<const ExplodedNode *, 32> Visited; llvm::SmallVector<const ExplodedNode *, 11> worklist;
ExplodedNodeSet Result; llvm::DenseMap<const ExplodedNode *, unsigned> visited;
WorkList.push_back(N); worklist.push_back(node);
while (!WorkList.empty()) { while (!worklist.empty()) {
const ExplodedNode *Head = WorkList.front(); node = worklist.back();
WorkList.pop_front(); worklist.pop_back();
Visited.insert(Head);
const ProgramPoint &PP = Head->getLocation(); // Was this node previously visited?
unsigned &visitFlag = visited[node];
if (visitFlag)
continue;
visitFlag = 1;
const ProgramPoint &PP = node->getLocation();
if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&PP)) { if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&PP)) {
// Get the CFGBlock and test the reachability // Get the CFGBlock and test the reachability
const CFGBlock *CB = BE->getBlock(); const CFGBlock *CB = BE->getBlock();
// If we cannot reach the beginning CFGBlock from this block, then we are // If we cannot reach the beginning CFGBlock from this block, then we are
// finished // finished
if (!CRA.isReachable(CB, Begin)) { if (!CRA->isReachable(CB, Begin)) {
Result.Add(const_cast<ExplodedNode *>(Head)); result.Add(const_cast<ExplodedNode *>(node));
continue; continue;
} }
} }
// Add unvisited children to the worklist // Add unvisited children to the worklist
for (ExplodedNode::const_succ_iterator I = Head->succ_begin(), for (ExplodedNode::const_succ_iterator i = node->succ_begin(),
E = Head->succ_end(); I != E; ++I) e = node->succ_end(); i != e; ++i)
if (!Visited.count(*I)) worklist.push_back(*i);
WorkList.push_back(*I);
} }
// Return the ExplodedNodes that were found
return Result;
} }
bool IdempotentOperationChecker::CFGReachabilityAnalysis::isReachable( bool IdempotentOperationChecker::CFGReachabilityAnalysis::isReachable(
@ -797,45 +806,51 @@ bool IdempotentOperationChecker::CFGReachabilityAnalysis::isReachable(
const unsigned DstBlockID = Dst->getBlockID(); const unsigned DstBlockID = Dst->getBlockID();
// If we haven't analyzed the destination node, run the analysis now // If we haven't analyzed the destination node, run the analysis now
if (!analyzed.count(DstBlockID)) { if (!analyzed[DstBlockID]) {
MapReachability(Dst); MapReachability(Dst);
analyzed.insert(DstBlockID); analyzed[DstBlockID] = true;
} }
// Return the cached result // Return the cached result
return reachable[DstBlockID].count(Src->getBlockID()); return reachable[DstBlockID][Src->getBlockID()];
} }
// Maps reachability to a common node by walking the predecessors of the // Maps reachability to a common node by walking the predecessors of the
// destination node. // destination node.
void IdempotentOperationChecker::CFGReachabilityAnalysis::MapReachability( void IdempotentOperationChecker::CFGReachabilityAnalysis::MapReachability(
const CFGBlock *Dst) { const CFGBlock *Dst) {
std::deque<const CFGBlock *> WorkList;
// Maintain a visited list to ensure we don't get stuck on cycles llvm::SmallVector<const CFGBlock *, 11> worklist;
llvm::SmallSet<unsigned, 32> Visited; llvm::BitVector visited(analyzed.size());
ReachableSet &DstReachability = reachable[Dst->getBlockID()]; ReachableSet &DstReachability = reachable[Dst->getBlockID()];
DstReachability.resize(analyzed.size(), false);
// Start searching from the destination node, since we commonly will perform // Start searching from the destination node, since we commonly will perform
// multiple queries relating to a destination node. // multiple queries relating to a destination node.
WorkList.push_back(Dst); worklist.push_back(Dst);
bool firstRun = true; bool firstRun = true;
while (!WorkList.empty()) {
const CFGBlock *Head = WorkList.front(); while (!worklist.empty()) {
WorkList.pop_front(); const CFGBlock *block = worklist.back();
Visited.insert(Head->getBlockID()); worklist.pop_back();
if (visited[block->getBlockID()])
continue;
visited[block->getBlockID()] = true;
// Update reachability information for this node -> Dst // Update reachability information for this node -> Dst
if (!firstRun) if (!firstRun) {
// Don't insert Dst -> Dst unless it was a predecessor of itself // Don't insert Dst -> Dst unless it was a predecessor of itself
DstReachability.insert(Head->getBlockID()); DstReachability[block->getBlockID()] = true;
}
else else
firstRun = false; firstRun = false;
// Add the predecessors to the worklist unless we have already visited them // Add the predecessors to the worklist.
for (CFGBlock::const_pred_iterator I = Head->pred_begin(); for (CFGBlock::const_pred_iterator i = block->pred_begin(),
I != Head->pred_end(); ++I) e = block->pred_end(); i != e; ++i) {
if (!Visited.count((*I)->getBlockID())) worklist.push_back(*i);
WorkList.push_back(*I); }
} }
} }