2007-09-21 05:42:55 +08:00
|
|
|
//=- LiveVariables.cpp - Live Variable Analysis for Source CFGs -*- C++ --*-==//
|
2007-09-06 08:17:54 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Ted Kremenek and is distributed under
|
2007-09-25 12:31:27 +08:00
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for
|
|
|
|
// details.
|
2007-09-06 08:17:54 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements Live Variables analysis for source-level CFGs.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Analysis/LiveVariables.h"
|
2007-09-07 05:26:58 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2007-09-06 08:17:54 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/CFG.h"
|
2007-09-25 12:31:27 +08:00
|
|
|
#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
|
|
|
|
#include "DataflowSolver.h"
|
2007-09-06 08:17:54 +08:00
|
|
|
#include "clang/Lex/IdentifierTable.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
|
2007-09-07 05:26:58 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2007-09-06 08:17:54 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-25 12:31:27 +08:00
|
|
|
// Dataflow initialization logic.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-06 08:17:54 +08:00
|
|
|
|
|
|
|
namespace {
|
2007-09-25 12:31:27 +08:00
|
|
|
struct RegisterDecls : public CFGRecStmtDeclVisitor<RegisterDecls> {
|
|
|
|
LiveVariables::AnalysisDataTy& AD;
|
|
|
|
void VisitVarDecl(VarDecl* VD) { AD.RegisterDecl(VD); }
|
2007-09-06 08:17:54 +08:00
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
RegisterDecls(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2007-09-06 08:17:54 +08:00
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
void LiveVariables::InitializeValues(const CFG& cfg) {
|
|
|
|
RegisterDecls R(getAnalysisData());
|
|
|
|
cfg.VisitBlockStmts(R);
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-25 12:31:27 +08:00
|
|
|
// Transfer functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-06 08:17:54 +08:00
|
|
|
|
|
|
|
namespace {
|
2007-09-25 12:31:27 +08:00
|
|
|
class TransferFuncs : public CFGStmtVisitor<TransferFuncs> {
|
|
|
|
LiveVariables::AnalysisDataTy& AD;
|
|
|
|
LiveVariables::ValTy Live;
|
2007-09-06 08:17:54 +08:00
|
|
|
public:
|
2007-09-25 12:31:27 +08:00
|
|
|
TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
|
2007-09-06 08:17:54 +08:00
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
LiveVariables::ValTy& getVal() { return Live; }
|
2007-09-13 03:10:52 +08:00
|
|
|
|
2007-09-06 08:17:54 +08:00
|
|
|
void VisitDeclRefExpr(DeclRefExpr* DR);
|
|
|
|
void VisitBinaryOperator(BinaryOperator* B);
|
|
|
|
void VisitAssign(BinaryOperator* B);
|
2007-09-07 05:26:58 +08:00
|
|
|
void VisitDeclStmt(DeclStmt* DS);
|
|
|
|
void VisitUnaryOperator(UnaryOperator* U);
|
2007-09-25 12:31:27 +08:00
|
|
|
void VisitStmt(Stmt* S) { VisitChildren(S); }
|
|
|
|
void Visit(Stmt *S) {
|
|
|
|
if (AD.Observer) AD.Observer->ObserveStmt(S,AD,Live);
|
|
|
|
static_cast<CFGStmtVisitor<TransferFuncs>*>(this)->Visit(S);
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
|
|
|
|
if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
|
|
|
|
Live.set(AD[V]); // Register a use of the variable.
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
|
2007-09-13 03:10:52 +08:00
|
|
|
if (B->isAssignmentOp()) VisitAssign(B);
|
|
|
|
else VisitStmt(B);
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
|
2007-09-07 05:26:58 +08:00
|
|
|
switch (U->getOpcode()) {
|
2007-09-25 12:31:27 +08:00
|
|
|
case UnaryOperator::PostInc:
|
|
|
|
case UnaryOperator::PostDec:
|
|
|
|
case UnaryOperator::PreInc:
|
|
|
|
case UnaryOperator::PreDec:
|
|
|
|
case UnaryOperator::AddrOf:
|
|
|
|
// Walk through the subexpressions, blasting through ParenExprs
|
|
|
|
// until we either find a DeclRefExpr or some non-DeclRefExpr
|
|
|
|
// expression.
|
|
|
|
for (Stmt* S = U->getSubExpr() ;;) {
|
|
|
|
if (ParenExpr* P = dyn_cast<ParenExpr>(S)) { S=P->getSubExpr(); continue;}
|
|
|
|
else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) {
|
|
|
|
// Treat the --/++/& operator as a kill.
|
|
|
|
Live.reset(AD[DR->getDecl()]);
|
|
|
|
if (AD.Observer) AD.Observer->ObserverKill(DR);
|
|
|
|
VisitDeclRefExpr(DR);
|
|
|
|
}
|
|
|
|
else Visit(S);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Visit(U->getSubExpr());
|
|
|
|
break;
|
2007-09-07 05:26:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
void TransferFuncs::VisitAssign(BinaryOperator* B) {
|
2007-09-06 08:17:54 +08:00
|
|
|
Stmt* LHS = B->getLHS();
|
2007-09-25 12:31:27 +08:00
|
|
|
|
|
|
|
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) { // Assigning to a var?
|
|
|
|
Live.reset(AD[DR->getDecl()]);
|
|
|
|
if (AD.Observer) AD.Observer->ObserverKill(DR);
|
|
|
|
// Handle things like +=, etc., which also generate "uses"
|
|
|
|
// of a variable. Do this just by visiting the subexpression.
|
|
|
|
if (B->getOpcode() != BinaryOperator::Assign) Visit(LHS);
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
2007-09-25 12:31:27 +08:00
|
|
|
else // Not assigning to a variable. Process LHS as usual.
|
2007-09-07 05:26:58 +08:00
|
|
|
Visit(LHS);
|
2007-09-06 08:17:54 +08:00
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
Visit(B->getRHS());
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
|
|
|
|
// Declarations effectively "kill" a variable since they cannot
|
|
|
|
// possibly be live before they are declared.
|
2007-09-14 07:52:58 +08:00
|
|
|
for (ScopedDecl* D = DS->getDecl(); D != NULL ; D = D->getNextDeclarator())
|
2007-09-25 12:31:27 +08:00
|
|
|
Live.reset(AD[D]);
|
2007-09-07 05:26:58 +08:00
|
|
|
}
|
2007-09-25 12:31:27 +08:00
|
|
|
} // end anonymous namespace
|
2007-09-06 08:17:54 +08:00
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
namespace {
|
|
|
|
struct Merge {
|
|
|
|
void operator()(LiveVariables::ValTy& Dst, LiveVariables::ValTy& Src) {
|
|
|
|
Src |= Dst;
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
2007-09-25 12:31:27 +08:00
|
|
|
};
|
2007-09-06 08:17:54 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
namespace {
|
|
|
|
typedef DataflowSolver<LiveVariables,TransferFuncs,Merge> Solver;
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
void LiveVariables::runOnCFG(const CFG& cfg) {
|
|
|
|
Solver S(*this);
|
|
|
|
S.runOnCFG(cfg);
|
|
|
|
}
|
2007-09-07 05:26:58 +08:00
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
void LiveVariables::runOnAllBlocks(const CFG& cfg,
|
|
|
|
LiveVariables::ObserverTy& Obs) {
|
|
|
|
Solver S(*this);
|
|
|
|
ObserverTy* OldObserver = getAnalysisData().Observer;
|
|
|
|
getAnalysisData().Observer = &Obs;
|
|
|
|
S.runOnAllBlocks(cfg);
|
|
|
|
getAnalysisData().Observer = OldObserver;
|
2007-09-07 05:26:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// liveness queries
|
|
|
|
//
|
|
|
|
|
2007-09-11 01:36:42 +08:00
|
|
|
bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
|
2007-09-25 12:31:27 +08:00
|
|
|
return getBlockData(B)[ getAnalysisData()[D] ];
|
2007-09-07 05:26:58 +08:00
|
|
|
}
|
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
|
|
|
|
return Live[ getAnalysisData()[D] ];
|
2007-09-07 07:00:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-06 08:17:54 +08:00
|
|
|
// printing liveness state for debugging
|
|
|
|
//
|
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
|
|
|
|
const AnalysisDataTy& AD = getAnalysisData();
|
|
|
|
|
|
|
|
for (AnalysisDataTy::iterator I = AD.begin(), E = AD.end(); I!=E; ++I)
|
|
|
|
if (V[I->second]) {
|
2007-09-07 05:26:58 +08:00
|
|
|
SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
|
2007-09-25 12:31:27 +08:00
|
|
|
|
2007-09-07 05:26:58 +08:00
|
|
|
fprintf(stderr, " %s <%s:%u:%u>\n",
|
|
|
|
I->first->getIdentifier()->getName(),
|
|
|
|
SM.getSourceName(PhysLoc),
|
|
|
|
SM.getLineNumber(PhysLoc),
|
|
|
|
SM.getColumnNumber(PhysLoc));
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-07 05:26:58 +08:00
|
|
|
void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
|
2007-09-25 12:31:27 +08:00
|
|
|
for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
|
|
|
|
E = getBlockDataMap().end(); I!=E; ++I) {
|
|
|
|
fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
|
2007-09-07 05:26:58 +08:00
|
|
|
I->first->getBlockID());
|
|
|
|
|
|
|
|
dumpLiveness(I->second,M);
|
|
|
|
}
|
2007-09-11 01:36:42 +08:00
|
|
|
|
|
|
|
fprintf(stderr,"\n");
|
|
|
|
}
|