2007-12-22 05:42:19 +08:00
|
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
2011-07-29 07:07:59 +08:00
|
|
|
#include "clang/AST/Stmt.h"
|
2009-07-17 02:13:04 +08:00
|
|
|
#include "clang/Analysis/CFG.h"
|
2010-01-25 12:41:41 +08:00
|
|
|
#include "clang/Analysis/AnalysisContext.h"
|
2011-07-29 07:07:59 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
|
|
|
|
|
|
|
#include <deque>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
2007-09-06 08:17:54 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
namespace {
|
|
|
|
class LiveVariablesImpl {
|
|
|
|
public:
|
|
|
|
AnalysisContext &analysisContext;
|
|
|
|
std::vector<LiveVariables::LivenessValues> cfgBlockValues;
|
|
|
|
llvm::ImmutableSet<const Stmt *>::Factory SSetFact;
|
|
|
|
llvm::ImmutableSet<const VarDecl *>::Factory DSetFact;
|
|
|
|
llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues> blocksEndToLiveness;
|
|
|
|
llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues> blocksBeginToLiveness;
|
|
|
|
llvm::DenseMap<const Stmt *, LiveVariables::LivenessValues> stmtsToLiveness;
|
|
|
|
llvm::DenseMap<const DeclRefExpr *, unsigned> inAssignment;
|
|
|
|
const bool killAtAssign;
|
|
|
|
|
|
|
|
LiveVariables::LivenessValues
|
|
|
|
merge(LiveVariables::LivenessValues valsA,
|
|
|
|
LiveVariables::LivenessValues valsB);
|
|
|
|
|
|
|
|
LiveVariables::LivenessValues runOnBlock(const CFGBlock *block,
|
|
|
|
LiveVariables::LivenessValues val,
|
|
|
|
LiveVariables::Observer *obs = 0);
|
|
|
|
|
|
|
|
void dumpBlockLiveness(const SourceManager& M);
|
|
|
|
|
|
|
|
LiveVariablesImpl(AnalysisContext &ac, bool KillAtAssign)
|
|
|
|
: analysisContext(ac), killAtAssign(KillAtAssign) {}
|
|
|
|
};
|
|
|
|
}
|
2008-04-16 02:35:30 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
static LiveVariablesImpl &getImpl(void* x) {
|
|
|
|
return *((LiveVariablesImpl *) x);
|
|
|
|
}
|
2008-04-16 02:35:30 +08:00
|
|
|
|
2007-09-06 08:17:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2011-07-29 07:07:59 +08:00
|
|
|
// Operations and queries on LivenessValues.
|
2009-09-09 23:08:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-06 08:17:54 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
bool LiveVariables::LivenessValues::isLive(const Stmt *S) const {
|
|
|
|
return liveStmts.contains(S);
|
|
|
|
}
|
2008-04-16 02:35:30 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
bool LiveVariables::LivenessValues::isLive(const VarDecl *D) const {
|
|
|
|
return liveDecls.contains(D);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
namespace {
|
|
|
|
template <typename SET>
|
|
|
|
SET mergeSets(typename SET::Factory &F, SET A, SET B) {
|
|
|
|
for (typename SET::iterator it = B.begin(), ei = B.end(); it != ei; ++it) {
|
|
|
|
A = F.add(A, *it);
|
|
|
|
}
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
}
|
2008-04-16 02:35:30 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
LiveVariables::LivenessValues
|
|
|
|
LiveVariablesImpl::merge(LiveVariables::LivenessValues valsA,
|
|
|
|
LiveVariables::LivenessValues valsB) {
|
|
|
|
return LiveVariables::LivenessValues(mergeSets(SSetFact, valsA.liveStmts, valsB.liveStmts),
|
|
|
|
mergeSets(DSetFact, valsA.liveDecls, valsB.liveDecls));
|
|
|
|
}
|
2008-04-16 02:35:30 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
bool LiveVariables::LivenessValues::equals(const LivenessValues &V) const {
|
|
|
|
return liveStmts == V.liveStmts && liveDecls == V.liveDecls;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Query methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-16 02:35:30 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
static bool isAlwaysAlive(const VarDecl *D) {
|
|
|
|
return D->hasGlobalStorage();
|
|
|
|
}
|
2008-06-18 02:05:57 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
bool LiveVariables::isLive(const CFGBlock *B, const VarDecl *D) {
|
|
|
|
return isAlwaysAlive(D) || getImpl(impl).blocksEndToLiveness[B].isLive(D);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
bool LiveVariables::isLive(const Stmt *S, const VarDecl *D) {
|
|
|
|
return isAlwaysAlive(D) || getImpl(impl).stmtsToLiveness[S].isLive(D);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
bool LiveVariables::isLive(const Stmt *Loc, const Stmt *S) {
|
|
|
|
return getImpl(impl).stmtsToLiveness[Loc].isLive(S);
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2011-07-29 07:07:59 +08:00
|
|
|
// Dataflow computation.
|
2009-09-09 23:08:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-06 08:17:54 +08:00
|
|
|
|
|
|
|
namespace {
|
2011-07-29 07:07:59 +08:00
|
|
|
class Worklist {
|
|
|
|
llvm::BitVector isBlockEnqueued;
|
|
|
|
std::deque<const CFGBlock *> workListContents;
|
2007-09-06 08:17:54 +08:00
|
|
|
public:
|
2011-07-29 07:07:59 +08:00
|
|
|
Worklist(CFG &cfg) : isBlockEnqueued(cfg.getNumBlockIDs()) {}
|
2009-12-24 10:40:30 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
bool empty() const { return workListContents.empty(); }
|
|
|
|
|
|
|
|
const CFGBlock *getNextItem() {
|
|
|
|
const CFGBlock *block = workListContents.front();
|
|
|
|
workListContents.pop_front();
|
|
|
|
isBlockEnqueued[block->getBlockID()] = false;
|
|
|
|
return block;
|
2008-04-16 02:35:30 +08:00
|
|
|
}
|
2011-02-12 07:24:26 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
void enqueueBlock(const CFGBlock *block) {
|
|
|
|
if (!isBlockEnqueued[block->getBlockID()]) {
|
|
|
|
isBlockEnqueued[block->getBlockID()] = true;
|
|
|
|
workListContents.push_back(block);
|
|
|
|
}
|
2011-02-12 07:24:26 +08:00
|
|
|
}
|
2007-09-06 08:17:54 +08:00
|
|
|
};
|
2011-07-29 07:07:59 +08:00
|
|
|
|
|
|
|
class TransferFunctions : public StmtVisitor<TransferFunctions> {
|
|
|
|
LiveVariablesImpl &LV;
|
|
|
|
LiveVariables::LivenessValues &val;
|
|
|
|
LiveVariables::Observer *observer;
|
|
|
|
const CFGBlock *currentBlock;
|
|
|
|
public:
|
|
|
|
TransferFunctions(LiveVariablesImpl &im,
|
|
|
|
LiveVariables::LivenessValues &Val,
|
|
|
|
LiveVariables::Observer *Observer,
|
|
|
|
const CFGBlock *CurrentBlock)
|
|
|
|
: LV(im), val(Val), observer(Observer), currentBlock(CurrentBlock) {}
|
|
|
|
|
|
|
|
void VisitBinaryOperator(BinaryOperator *BO);
|
|
|
|
void VisitBlockExpr(BlockExpr *BE);
|
|
|
|
void VisitDeclRefExpr(DeclRefExpr *DR);
|
|
|
|
void VisitDeclStmt(DeclStmt *DS);
|
|
|
|
void VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS);
|
|
|
|
void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE);
|
|
|
|
void VisitUnaryOperator(UnaryOperator *UO);
|
|
|
|
void Visit(Stmt *S);
|
|
|
|
};
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
void TransferFunctions::Visit(Stmt *S) {
|
|
|
|
if (observer)
|
|
|
|
observer->observeStmt(S, currentBlock, val);
|
|
|
|
|
|
|
|
StmtVisitor<TransferFunctions>::Visit(S);
|
|
|
|
|
|
|
|
if (isa<Expr>(S)) {
|
|
|
|
val.liveStmts = LV.SSetFact.remove(val.liveStmts, S);
|
2008-01-18 04:48:37 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
// Mark all children expressions live.
|
|
|
|
|
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case Stmt::StmtExprClass: {
|
|
|
|
// For statement expressions, look through the compound statement.
|
|
|
|
S = cast<StmtExpr>(S)->getSubStmt();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Stmt::CXXMemberCallExprClass: {
|
|
|
|
// Include the implicit "this" pointer as being live.
|
|
|
|
CXXMemberCallExpr *CE = cast<CXXMemberCallExpr>(S);
|
|
|
|
val.liveStmts =
|
|
|
|
LV.SSetFact.add(val.liveStmts,
|
|
|
|
CE->getImplicitObjectArgument()->IgnoreParens());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// FIXME: These cases eventually shouldn't be needed.
|
|
|
|
case Stmt::ExprWithCleanupsClass: {
|
|
|
|
S = cast<ExprWithCleanups>(S)->getSubExpr();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Stmt::CXXBindTemporaryExprClass: {
|
|
|
|
S = cast<CXXBindTemporaryExpr>(S)->getSubExpr();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Stmt::MaterializeTemporaryExprClass: {
|
|
|
|
S = cast<MaterializeTemporaryExpr>(S)->GetTemporaryExpr();
|
|
|
|
break;
|
|
|
|
}
|
2009-06-30 20:11:58 +08:00
|
|
|
}
|
2009-12-24 10:40:30 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
for (Stmt::child_iterator it = S->child_begin(), ei = S->child_end();
|
|
|
|
it != ei; ++it) {
|
|
|
|
if (Stmt *child = *it) {
|
|
|
|
if (Expr *Ex = dyn_cast<Expr>(child))
|
|
|
|
child = Ex->IgnoreParens();
|
|
|
|
|
|
|
|
val.liveStmts = LV.SSetFact.add(val.liveStmts, child);
|
|
|
|
}
|
|
|
|
}
|
2009-12-24 10:40:30 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
void TransferFunctions::VisitBinaryOperator(BinaryOperator *B) {
|
|
|
|
if (B->isAssignmentOp()) {
|
|
|
|
if (!LV.killAtAssign)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Assigning to a variable?
|
|
|
|
Expr *LHS = B->getLHS()->IgnoreParens();
|
|
|
|
|
|
|
|
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS))
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
|
|
|
|
// Assignments to references don't kill the ref's address
|
|
|
|
if (VD->getType()->isReferenceType())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!isAlwaysAlive(VD)) {
|
|
|
|
// The variable is now dead.
|
|
|
|
val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (observer)
|
|
|
|
observer->observerKill(DR);
|
|
|
|
}
|
|
|
|
}
|
2008-04-15 12:39:08 +08:00
|
|
|
}
|
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
void TransferFunctions::VisitBlockExpr(BlockExpr *BE) {
|
2009-11-26 10:31:33 +08:00
|
|
|
AnalysisContext::referenced_decls_iterator I, E;
|
2011-07-29 07:07:59 +08:00
|
|
|
llvm::tie(I, E) =
|
|
|
|
LV.analysisContext.getReferencedBlockVars(BE->getBlockDecl());
|
2009-11-26 10:31:33 +08:00
|
|
|
for ( ; I != E ; ++I) {
|
2011-07-29 07:07:59 +08:00
|
|
|
const VarDecl *VD = *I;
|
|
|
|
if (isAlwaysAlive(VD))
|
|
|
|
continue;
|
|
|
|
val.liveDecls = LV.DSetFact.add(val.liveDecls, VD);
|
2009-11-26 10:31:33 +08:00
|
|
|
}
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *DR) {
|
|
|
|
if (const VarDecl *D = dyn_cast<VarDecl>(DR->getDecl()))
|
|
|
|
if (!isAlwaysAlive(D) && LV.inAssignment.find(DR) == LV.inAssignment.end())
|
|
|
|
val.liveDecls = LV.DSetFact.add(val.liveDecls, D);
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
void TransferFunctions::VisitDeclStmt(DeclStmt *DS) {
|
|
|
|
for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE = DS->decl_end();
|
|
|
|
DI != DE; ++DI)
|
|
|
|
if (VarDecl* VD = dyn_cast<VarDecl>(*DI)) {
|
|
|
|
if (!isAlwaysAlive(VD))
|
|
|
|
val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS) {
|
|
|
|
// Kill the iteration variable.
|
|
|
|
DeclRefExpr *DR = 0;
|
|
|
|
const VarDecl *VD = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
Stmt *element = OS->getElement();
|
|
|
|
if (DeclStmt *DS = dyn_cast<DeclStmt>(element)) {
|
2009-03-28 14:33:19 +08:00
|
|
|
VD = cast<VarDecl>(DS->getSingleDecl());
|
2008-11-12 01:42:10 +08:00
|
|
|
}
|
2011-07-29 07:07:59 +08:00
|
|
|
else if ((DR = dyn_cast<DeclRefExpr>(cast<Expr>(element)->IgnoreParens()))) {
|
|
|
|
VD = cast<VarDecl>(DR->getDecl());
|
|
|
|
}
|
|
|
|
|
2008-11-14 09:58:12 +08:00
|
|
|
if (VD) {
|
2011-07-29 07:07:59 +08:00
|
|
|
val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
|
|
|
|
if (observer && DR)
|
|
|
|
observer->observerKill(DR);
|
2008-11-14 09:58:12 +08:00
|
|
|
}
|
2008-11-12 01:42:10 +08:00
|
|
|
}
|
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
void TransferFunctions::
|
|
|
|
VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE)
|
|
|
|
{
|
|
|
|
// While sizeof(var) doesn't technically extend the liveness of 'var', it
|
|
|
|
// does extent the liveness of metadata if 'var' is a VariableArrayType.
|
|
|
|
// We handle that special case here.
|
|
|
|
if (UE->getKind() != UETT_SizeOf || UE->isArgumentType())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const DeclRefExpr *DR =
|
|
|
|
dyn_cast<DeclRefExpr>(UE->getArgumentExpr()->IgnoreParens());
|
|
|
|
|
|
|
|
if (!DR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
if (VD && VD->getType()->isVariableArrayType())
|
|
|
|
val.liveDecls = LV.DSetFact.add(val.liveDecls, VD);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
void TransferFunctions::VisitUnaryOperator(UnaryOperator *UO) {
|
|
|
|
// Treat ++/-- as a kill.
|
|
|
|
// Note we don't actually have to do anything if we don't have an observer,
|
|
|
|
// since a ++/-- acts as both a kill and a "use".
|
|
|
|
if (!observer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (UO->getOpcode()) {
|
|
|
|
default:
|
|
|
|
return;
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_PostInc:
|
2011-07-29 07:07:59 +08:00
|
|
|
case UO_PostDec:
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_PreInc:
|
|
|
|
case UO_PreDec:
|
2011-07-29 07:07:59 +08:00
|
|
|
break;
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
2011-07-29 07:07:59 +08:00
|
|
|
|
|
|
|
if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(UO->getSubExpr()->IgnoreParens()))
|
|
|
|
if (isa<VarDecl>(DR->getDecl())) {
|
|
|
|
// Treat ++/-- as a kill.
|
|
|
|
observer->observerKill(DR);
|
2008-02-26 06:28:54 +08:00
|
|
|
}
|
2007-09-29 04:38:59 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
LiveVariables::LivenessValues
|
|
|
|
LiveVariablesImpl::runOnBlock(const CFGBlock *block,
|
|
|
|
LiveVariables::LivenessValues val,
|
|
|
|
LiveVariables::Observer *obs) {
|
2007-09-06 08:17:54 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
TransferFunctions TF(*this, val, obs, block);
|
|
|
|
|
|
|
|
// Visit the terminator (if any).
|
|
|
|
if (const Stmt *term = block->getTerminator())
|
|
|
|
TF.Visit(const_cast<Stmt*>(term));
|
|
|
|
|
|
|
|
// Apply the transfer function for all Stmts in the block.
|
|
|
|
for (CFGBlock::const_reverse_iterator it = block->rbegin(),
|
|
|
|
ei = block->rend(); it != ei; ++it) {
|
|
|
|
const CFGElement &elem = *it;
|
|
|
|
if (!isa<CFGStmt>(elem))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const Stmt *S = cast<CFGStmt>(elem).getStmt();
|
|
|
|
TF.Visit(const_cast<Stmt*>(S));
|
|
|
|
stmtsToLiveness[S] = val;
|
|
|
|
}
|
|
|
|
return val;
|
2007-09-25 12:31:27 +08:00
|
|
|
}
|
2007-09-07 05:26:58 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
void LiveVariables::runOnAllBlocks(LiveVariables::Observer &obs) {
|
|
|
|
const CFG *cfg = getImpl(impl).analysisContext.getCFG();
|
|
|
|
for (CFG::const_iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it)
|
|
|
|
getImpl(impl).runOnBlock(*it, getImpl(impl).blocksEndToLiveness[*it], &obs);
|
2007-09-07 05:26:58 +08:00
|
|
|
}
|
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
LiveVariables::LiveVariables(void *im) : impl(im) {}
|
2007-09-07 05:26:58 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
LiveVariables::~LiveVariables() {
|
|
|
|
delete (LiveVariablesImpl*) impl;
|
2007-09-07 05:26:58 +08:00
|
|
|
}
|
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
LiveVariables *
|
|
|
|
LiveVariables::computeLiveness(AnalysisContext &AC,
|
|
|
|
bool killAtAssign) {
|
|
|
|
|
|
|
|
// No CFG? Bail out.
|
|
|
|
CFG *cfg = AC.getCFG();
|
|
|
|
if (!cfg)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
LiveVariablesImpl *LV = new LiveVariablesImpl(AC, killAtAssign);
|
|
|
|
|
|
|
|
// Construct the dataflow worklist. Enqueue the exit block as the
|
|
|
|
// start of the analysis.
|
|
|
|
Worklist worklist(*cfg);
|
|
|
|
llvm::BitVector everAnalyzedBlock(cfg->getNumBlockIDs());
|
|
|
|
|
|
|
|
// FIXME: we should enqueue using post order.
|
|
|
|
for (CFG::const_iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it) {
|
|
|
|
const CFGBlock *block = *it;
|
|
|
|
worklist.enqueueBlock(block);
|
|
|
|
|
|
|
|
// FIXME: Scan for DeclRefExprs using in the LHS of an assignment.
|
|
|
|
// We need to do this because we lack context in the reverse analysis
|
|
|
|
// to determine if a DeclRefExpr appears in such a context, and thus
|
|
|
|
// doesn't constitute a "use".
|
|
|
|
if (killAtAssign)
|
|
|
|
for (CFGBlock::const_iterator bi = block->begin(), be = block->end();
|
|
|
|
bi != be; ++bi) {
|
|
|
|
if (const CFGStmt *cs = bi->getAs<CFGStmt>()) {
|
|
|
|
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(cs->getStmt())) {
|
|
|
|
if (BO->getOpcode() == BO_Assign) {
|
|
|
|
if (const DeclRefExpr *DR =
|
|
|
|
dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens())) {
|
|
|
|
LV->inAssignment[DR] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!worklist.empty()) {
|
|
|
|
// Dequeue blocks in FIFO order.
|
|
|
|
const CFGBlock *block = worklist.getNextItem();
|
|
|
|
|
|
|
|
// Determine if the block's end value has changed. If not, we
|
|
|
|
// have nothing left to do for this block.
|
|
|
|
LivenessValues &prevVal = LV->blocksEndToLiveness[block];
|
|
|
|
|
|
|
|
// Merge the values of all successor blocks.
|
|
|
|
LivenessValues val;
|
|
|
|
for (CFGBlock::const_succ_iterator it = block->succ_begin(),
|
|
|
|
ei = block->succ_end(); it != ei; ++it) {
|
|
|
|
if (const CFGBlock *succ = *it)
|
|
|
|
val = LV->merge(val, LV->blocksBeginToLiveness[succ]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!everAnalyzedBlock[block->getBlockID()])
|
|
|
|
everAnalyzedBlock[block->getBlockID()] = true;
|
|
|
|
else if (prevVal.equals(val))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
prevVal = val;
|
|
|
|
|
|
|
|
// Update the dataflow value for the start of this block.
|
|
|
|
LV->blocksBeginToLiveness[block] = LV->runOnBlock(block, val);
|
|
|
|
|
|
|
|
// Enqueue the value to the predecessors.
|
|
|
|
for (CFGBlock::const_pred_iterator it = block->pred_begin(),
|
|
|
|
ei = block->pred_end(); it != ei; ++it)
|
|
|
|
{
|
|
|
|
if (const CFGBlock *pred = *it)
|
|
|
|
worklist.enqueueBlock(pred);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new LiveVariables(LV);
|
2007-09-07 07:00:42 +08:00
|
|
|
}
|
|
|
|
|
2011-08-02 12:50:49 +08:00
|
|
|
static bool compare_entries(const CFGBlock *A, const CFGBlock *B) {
|
2011-07-29 07:07:59 +08:00
|
|
|
return A->getBlockID() < B->getBlockID();
|
|
|
|
}
|
2011-08-02 12:50:49 +08:00
|
|
|
|
|
|
|
static bool compare_vd_entries(const Decl *A, const Decl *B) {
|
2011-07-29 07:07:59 +08:00
|
|
|
SourceLocation ALoc = A->getLocStart();
|
|
|
|
SourceLocation BLoc = B->getLocStart();
|
|
|
|
return ALoc.getRawEncoding() < BLoc.getRawEncoding();
|
2008-01-18 04:48:37 +08:00
|
|
|
}
|
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
void LiveVariables::dumpBlockLiveness(const SourceManager &M) {
|
|
|
|
getImpl(impl).dumpBlockLiveness(M);
|
2008-01-18 08:40:21 +08:00
|
|
|
}
|
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
void LiveVariablesImpl::dumpBlockLiveness(const SourceManager &M) {
|
|
|
|
std::vector<const CFGBlock *> vec;
|
|
|
|
for (llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues>::iterator
|
|
|
|
it = blocksEndToLiveness.begin(), ei = blocksEndToLiveness.end();
|
|
|
|
it != ei; ++it) {
|
|
|
|
vec.push_back(it->first);
|
|
|
|
}
|
|
|
|
std::sort(vec.begin(), vec.end(), compare_entries);
|
2007-09-06 08:17:54 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
std::vector<const VarDecl*> declVec;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
for (std::vector<const CFGBlock *>::iterator
|
|
|
|
it = vec.begin(), ei = vec.end(); it != ei; ++it) {
|
|
|
|
llvm::errs() << "\n[ B" << (*it)->getBlockID()
|
|
|
|
<< " (live variables at block exit) ]\n";
|
|
|
|
|
|
|
|
LiveVariables::LivenessValues vals = blocksEndToLiveness[*it];
|
|
|
|
declVec.clear();
|
|
|
|
|
|
|
|
for (llvm::ImmutableSet<const VarDecl *>::iterator si =
|
|
|
|
vals.liveDecls.begin(),
|
|
|
|
se = vals.liveDecls.end(); si != se; ++si) {
|
|
|
|
declVec.push_back(*si);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(declVec.begin(), declVec.end(), compare_vd_entries);
|
|
|
|
|
|
|
|
for (std::vector<const VarDecl*>::iterator di = declVec.begin(),
|
|
|
|
de = declVec.end(); di != de; ++di) {
|
|
|
|
llvm::errs() << " " << (*di)->getDeclName().getAsString()
|
|
|
|
<< " <";
|
|
|
|
(*di)->getLocation().dump(M);
|
2009-10-18 02:12:37 +08:00
|
|
|
llvm::errs() << ">\n";
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
2007-09-07 05:26:58 +08:00
|
|
|
}
|
2011-07-29 07:07:59 +08:00
|
|
|
llvm::errs() << "\n";
|
2007-09-11 01:36:42 +08:00
|
|
|
}
|
2011-07-29 07:07:59 +08:00
|
|
|
|