2007-12-22 05:42:19 +08:00
|
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
2011-10-22 10:14:23 +08:00
|
|
|
#include "clang/Analysis/Analyses/PostOrderCFGView.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"
|
|
|
|
|
2011-09-17 07:01:39 +08:00
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
#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 {
|
2011-09-17 07:01:39 +08:00
|
|
|
|
|
|
|
class DataflowWorklist {
|
|
|
|
SmallVector<const CFGBlock *, 20> worklist;
|
|
|
|
llvm::BitVector enqueuedBlocks;
|
2011-10-22 10:14:23 +08:00
|
|
|
PostOrderCFGView *POV;
|
2011-09-17 07:01:39 +08:00
|
|
|
public:
|
2011-10-24 09:32:45 +08:00
|
|
|
DataflowWorklist(const CFG &cfg, AnalysisDeclContext &Ctx)
|
2011-09-17 07:01:39 +08:00
|
|
|
: enqueuedBlocks(cfg.getNumBlockIDs()),
|
2011-10-22 10:14:23 +08:00
|
|
|
POV(Ctx.getAnalysis<PostOrderCFGView>()) {}
|
2011-09-17 07:01:39 +08:00
|
|
|
|
|
|
|
void enqueueBlock(const CFGBlock *block);
|
|
|
|
void enqueueSuccessors(const CFGBlock *block);
|
|
|
|
void enqueuePredecessors(const CFGBlock *block);
|
|
|
|
|
|
|
|
const CFGBlock *dequeue();
|
|
|
|
|
|
|
|
void sortWorklist();
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataflowWorklist::enqueueBlock(const clang::CFGBlock *block) {
|
|
|
|
if (block && !enqueuedBlocks[block->getBlockID()]) {
|
|
|
|
enqueuedBlocks[block->getBlockID()] = true;
|
|
|
|
worklist.push_back(block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataflowWorklist::enqueueSuccessors(const clang::CFGBlock *block) {
|
|
|
|
const unsigned OldWorklistSize = worklist.size();
|
|
|
|
for (CFGBlock::const_succ_iterator I = block->succ_begin(),
|
|
|
|
E = block->succ_end(); I != E; ++I) {
|
|
|
|
enqueueBlock(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OldWorklistSize == 0 || OldWorklistSize == worklist.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
sortWorklist();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataflowWorklist::enqueuePredecessors(const clang::CFGBlock *block) {
|
|
|
|
const unsigned OldWorklistSize = worklist.size();
|
|
|
|
for (CFGBlock::const_pred_iterator I = block->pred_begin(),
|
|
|
|
E = block->pred_end(); I != E; ++I) {
|
|
|
|
enqueueBlock(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OldWorklistSize == 0 || OldWorklistSize == worklist.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
sortWorklist();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataflowWorklist::sortWorklist() {
|
2011-10-22 10:14:23 +08:00
|
|
|
std::sort(worklist.begin(), worklist.end(), POV->getComparator());
|
2011-09-17 07:01:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const CFGBlock *DataflowWorklist::dequeue() {
|
|
|
|
if (worklist.empty())
|
|
|
|
return 0;
|
|
|
|
const CFGBlock *b = worklist.back();
|
|
|
|
worklist.pop_back();
|
|
|
|
enqueuedBlocks[b->getBlockID()] = false;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class LiveVariablesImpl {
|
|
|
|
public:
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext &analysisContext;
|
2011-09-17 07:01:39 +08:00
|
|
|
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);
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
LiveVariablesImpl(AnalysisDeclContext &ac, bool KillAtAssign)
|
2011-10-02 09:45:37 +08:00
|
|
|
: analysisContext(ac),
|
|
|
|
SSetFact(false), // Do not canonicalize ImmutableSets by default.
|
|
|
|
DSetFact(false), // This is a *major* performance win.
|
|
|
|
killAtAssign(KillAtAssign) {}
|
2011-09-17 07:01:39 +08:00
|
|
|
};
|
2011-07-29 07:07:59 +08:00
|
|
|
}
|
2008-04-16 02:35:30 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
static LiveVariablesImpl &getImpl(void *x) {
|
2011-07-29 07:07:59 +08:00
|
|
|
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>
|
2011-09-17 07:01:39 +08:00
|
|
|
SET mergeSets(SET A, SET B) {
|
|
|
|
if (A.isEmpty())
|
|
|
|
return B;
|
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
for (typename SET::iterator it = B.begin(), ei = B.end(); it != ei; ++it) {
|
2011-09-17 07:01:39 +08:00
|
|
|
A = A.add(*it);
|
2011-07-29 07:07:59 +08:00
|
|
|
}
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
}
|
2008-04-16 02:35:30 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void LiveVariables::Observer::anchor() { }
|
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
LiveVariables::LivenessValues
|
|
|
|
LiveVariablesImpl::merge(LiveVariables::LivenessValues valsA,
|
|
|
|
LiveVariables::LivenessValues valsB) {
|
2011-09-17 07:01:39 +08:00
|
|
|
|
|
|
|
llvm::ImmutableSetRef<const Stmt *>
|
|
|
|
SSetRefA(valsA.liveStmts.getRootWithoutRetain(), SSetFact.getTreeFactory()),
|
|
|
|
SSetRefB(valsB.liveStmts.getRootWithoutRetain(), SSetFact.getTreeFactory());
|
|
|
|
|
|
|
|
|
|
|
|
llvm::ImmutableSetRef<const VarDecl *>
|
|
|
|
DSetRefA(valsA.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory()),
|
|
|
|
DSetRefB(valsB.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory());
|
|
|
|
|
|
|
|
|
|
|
|
SSetRefA = mergeSets(SSetRefA, SSetRefB);
|
|
|
|
DSetRefA = mergeSets(DSetRefA, DSetRefB);
|
|
|
|
|
2011-10-02 09:45:37 +08:00
|
|
|
// asImmutableSet() canonicalizes the tree, allowing us to do an easy
|
|
|
|
// comparison afterwards.
|
2011-09-17 07:01:39 +08:00
|
|
|
return LiveVariables::LivenessValues(SSetRefA.asImmutableSet(),
|
|
|
|
DSetRefA.asImmutableSet());
|
2011-07-29 07:07:59 +08:00
|
|
|
}
|
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 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-08-06 08:30:00 +08:00
|
|
|
static const VariableArrayType *FindVA(QualType Ty) {
|
|
|
|
const Type *ty = Ty.getTypePtr();
|
|
|
|
while (const ArrayType *VT = dyn_cast<ArrayType>(ty)) {
|
|
|
|
if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(VT))
|
|
|
|
if (VAT->getSizeExpr())
|
|
|
|
return VAT;
|
|
|
|
|
|
|
|
ty = VT->getElementType().getTypePtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-05 08:26:53 +08:00
|
|
|
static const Stmt *LookThroughStmt(const Stmt *S) {
|
2011-11-05 15:34:28 +08:00
|
|
|
while (S) {
|
|
|
|
if (const Expr *Ex = dyn_cast<Expr>(S))
|
|
|
|
S = Ex->IgnoreParens();
|
2011-11-10 01:10:36 +08:00
|
|
|
if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(S)) {
|
|
|
|
S = EWC->getSubExpr();
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-05 15:34:28 +08:00
|
|
|
if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(S)) {
|
|
|
|
S = OVE->getSourceExpr();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-11-05 08:26:53 +08:00
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AddLiveStmt(llvm::ImmutableSet<const Stmt *> &Set,
|
|
|
|
llvm::ImmutableSet<const Stmt *>::Factory &F,
|
|
|
|
const Stmt *S) {
|
|
|
|
Set = F.add(Set, LookThroughStmt(S));
|
|
|
|
}
|
|
|
|
|
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);
|
2011-10-07 04:53:28 +08:00
|
|
|
if (Expr *ImplicitObj = CE->getImplicitObjectArgument()) {
|
2011-11-05 08:26:53 +08:00
|
|
|
AddLiveStmt(val.liveStmts, LV.SSetFact, ImplicitObj);
|
2011-10-07 04:53:28 +08:00
|
|
|
}
|
2011-07-29 07:07:59 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-08-06 08:30:00 +08:00
|
|
|
case Stmt::DeclStmtClass: {
|
|
|
|
const DeclStmt *DS = cast<DeclStmt>(S);
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl())) {
|
|
|
|
for (const VariableArrayType* VA = FindVA(VD->getType());
|
|
|
|
VA != 0; VA = FindVA(VA->getElementType())) {
|
2011-11-05 08:26:53 +08:00
|
|
|
AddLiveStmt(val.liveStmts, LV.SSetFact, VA->getSizeExpr());
|
2011-08-06 08:30:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-11-06 17:01:30 +08:00
|
|
|
case Stmt::PseudoObjectExprClass: {
|
|
|
|
// A pseudo-object operation only directly consumes its result
|
|
|
|
// expression.
|
|
|
|
Expr *child = cast<PseudoObjectExpr>(S)->getResultExpr();
|
|
|
|
if (!child) return;
|
|
|
|
if (OpaqueValueExpr *OV = dyn_cast<OpaqueValueExpr>(child))
|
|
|
|
child = OV->getSourceExpr();
|
|
|
|
child = child->IgnoreParens();
|
|
|
|
val.liveStmts = LV.SSetFact.add(val.liveStmts, child);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-29 07:07:59 +08:00
|
|
|
// 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;
|
|
|
|
}
|
2011-08-06 08:30:00 +08:00
|
|
|
case Stmt::UnaryExprOrTypeTraitExprClass: {
|
|
|
|
// No need to unconditionally visit subexpressions.
|
|
|
|
return;
|
|
|
|
}
|
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) {
|
2011-11-05 08:26:53 +08:00
|
|
|
if (Stmt *child = *it)
|
|
|
|
AddLiveStmt(val.liveStmts, LV.SSetFact, child);
|
2011-07-29 07:07:59 +08:00
|
|
|
}
|
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();
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS))
|
2011-07-29 07:07:59 +08:00
|
|
|
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) {
|
2011-12-22 09:30:46 +08:00
|
|
|
AnalysisDeclContext::referenced_decls_iterator I, E;
|
|
|
|
llvm::tie(I, E) =
|
|
|
|
LV.analysisContext.getReferencedBlockVars(BE->getBlockDecl());
|
|
|
|
for ( ; I != E ; ++I) {
|
|
|
|
const VarDecl *VD = *I;
|
2011-07-29 07:07:59 +08:00
|
|
|
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)
|
2011-08-13 07:37:29 +08:00
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(*DI)) {
|
2011-07-29 07:07:59 +08:00
|
|
|
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;
|
|
|
|
|
2011-08-06 08:30:00 +08:00
|
|
|
const Expr *subEx = UE->getArgumentExpr();
|
|
|
|
if (subEx->getType()->isVariableArrayType()) {
|
|
|
|
assert(subEx->isLValue());
|
|
|
|
val.liveStmts = LV.SSetFact.add(val.liveStmts, subEx->IgnoreParens());
|
|
|
|
}
|
2011-07-29 07:07:59 +08:00
|
|
|
}
|
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 *
|
2011-10-24 09:32:45 +08:00
|
|
|
LiveVariables::computeLiveness(AnalysisDeclContext &AC,
|
2011-07-29 07:07:59 +08:00
|
|
|
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.
|
2011-10-22 10:14:23 +08:00
|
|
|
DataflowWorklist worklist(*cfg, AC);
|
2011-07-29 07:07:59 +08:00
|
|
|
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>()) {
|
2011-08-24 07:05:04 +08:00
|
|
|
if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(cs->getStmt())) {
|
2011-07-29 07:07:59 +08:00
|
|
|
if (BO->getOpcode() == BO_Assign) {
|
|
|
|
if (const DeclRefExpr *DR =
|
|
|
|
dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens())) {
|
|
|
|
LV->inAssignment[DR] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-17 07:01:39 +08:00
|
|
|
worklist.sortWorklist();
|
|
|
|
|
|
|
|
while (const CFGBlock *block = worklist.dequeue()) {
|
2011-07-29 07:07:59 +08:00
|
|
|
// 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) {
|
2011-09-17 07:01:39 +08:00
|
|
|
if (const CFGBlock *succ = *it) {
|
2011-07-29 07:07:59 +08:00
|
|
|
val = LV->merge(val, LV->blocksBeginToLiveness[succ]);
|
2011-09-17 07:01:39 +08:00
|
|
|
}
|
2011-07-29 07:07:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2011-09-17 07:01:39 +08:00
|
|
|
worklist.enqueuePredecessors(block);
|
2011-07-29 07:07:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-10-08 06:21:02 +08:00
|
|
|
const void *LiveVariables::getTag() { static int x; return &x; }
|
|
|
|
const void *RelaxedLiveVariables::getTag() { static int x; return &x; }
|