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
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-12-22 05:42:19 +08:00
|
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
2007-09-07 05:26:58 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2008-12-09 08:14:14 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2007-09-06 08:17:54 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2009-07-17 02:13:04 +08:00
|
|
|
#include "clang/Analysis/CFG.h"
|
2007-09-25 12:31:27 +08:00
|
|
|
#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
|
2007-09-26 05:00:24 +08:00
|
|
|
#include "clang/Analysis/FlowSensitive/DataflowSolver.h"
|
2007-09-06 08:17:54 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2008-04-16 02:35:30 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2008-01-09 02:19:08 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2007-09-06 08:17:54 +08:00
|
|
|
|
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;
|
|
|
|
|
2008-04-16 02:35:30 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Useful constants.
|
2009-09-09 23:08:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-16 02:35:30 +08:00
|
|
|
|
|
|
|
static const bool Alive = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
static const bool Dead = false;
|
2008-04-16 02:35:30 +08:00
|
|
|
|
2007-09-06 08:17:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-25 12:31:27 +08:00
|
|
|
// Dataflow initialization logic.
|
2009-09-09 23:08:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-06 08:17:54 +08:00
|
|
|
|
|
|
|
namespace {
|
2009-09-09 23:08:12 +08:00
|
|
|
class VISIBILITY_HIDDEN RegisterDecls
|
2008-01-09 02:19:08 +08:00
|
|
|
: public CFGRecStmtDeclVisitor<RegisterDecls> {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
LiveVariables::AnalysisDataTy& AD;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 02:35:30 +08:00
|
|
|
typedef llvm::SmallVector<VarDecl*, 20> AlwaysLiveTy;
|
|
|
|
AlwaysLiveTy AlwaysLive;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-29 04:38:59 +08:00
|
|
|
public:
|
2008-04-16 02:35:30 +08:00
|
|
|
RegisterDecls(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
|
|
|
|
|
|
|
|
~RegisterDecls() {
|
|
|
|
|
|
|
|
AD.AlwaysLive.resetValues(AD);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 02:35:30 +08:00
|
|
|
for (AlwaysLiveTy::iterator I = AlwaysLive.begin(), E = AlwaysLive.end();
|
2009-09-09 23:08:12 +08:00
|
|
|
I != E; ++ I)
|
|
|
|
AD.AlwaysLive(*I, AD) = Alive;
|
2008-04-16 02:35:30 +08:00
|
|
|
}
|
|
|
|
|
2008-06-18 02:05:57 +08:00
|
|
|
void VisitImplicitParamDecl(ImplicitParamDecl* IPD) {
|
|
|
|
// Register the VarDecl for tracking.
|
|
|
|
AD.Register(IPD);
|
|
|
|
}
|
|
|
|
|
2008-04-16 02:35:30 +08:00
|
|
|
void VisitVarDecl(VarDecl* VD) {
|
|
|
|
// Register the VarDecl for tracking.
|
|
|
|
AD.Register(VD);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 02:35:30 +08:00
|
|
|
// Does the variable have global storage? If so, it is always live.
|
|
|
|
if (VD->hasGlobalStorage())
|
2009-09-09 23:08:12 +08:00
|
|
|
AlwaysLive.push_back(VD);
|
2008-04-16 02:35:30 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-20 11:01:58 +08:00
|
|
|
CFG& getCFG() { return AD.getCFG(); }
|
2007-10-02 04:33:52 +08:00
|
|
|
};
|
2007-09-25 12:31:27 +08:00
|
|
|
} // end anonymous namespace
|
2007-09-06 08:17:54 +08:00
|
|
|
|
2008-12-09 08:14:14 +08:00
|
|
|
LiveVariables::LiveVariables(ASTContext& Ctx, CFG& cfg) {
|
2008-03-14 00:55:07 +08:00
|
|
|
// Register all referenced VarDecls.
|
2008-12-09 08:14:14 +08:00
|
|
|
getAnalysisData().setCFG(cfg);
|
|
|
|
getAnalysisData().setContext(Ctx);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-02 04:33:52 +08:00
|
|
|
RegisterDecls R(getAnalysisData());
|
2007-09-25 12:31:27 +08:00
|
|
|
cfg.VisitBlockStmts(R);
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-25 12:31:27 +08:00
|
|
|
// Transfer functions.
|
2009-09-09 23:08:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-06 08:17:54 +08:00
|
|
|
|
|
|
|
namespace {
|
2007-09-29 04:38:59 +08:00
|
|
|
|
2008-01-09 02:19:08 +08:00
|
|
|
class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
|
2007-09-25 12:31:27 +08:00
|
|
|
LiveVariables::AnalysisDataTy& AD;
|
2007-09-29 04:38:59 +08:00
|
|
|
LiveVariables::ValTy LiveState;
|
2007-09-06 08:17:54 +08:00
|
|
|
public:
|
2007-10-02 04:33:52 +08:00
|
|
|
TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
|
2007-09-06 08:17:54 +08:00
|
|
|
|
2007-09-29 04:38:59 +08:00
|
|
|
LiveVariables::ValTy& getVal() { return LiveState; }
|
2007-11-20 11:01:58 +08:00
|
|
|
CFG& getCFG() { return AD.getCFG(); }
|
2009-09-09 23:08:12 +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);
|
2008-11-13 05:58:46 +08:00
|
|
|
void BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S);
|
2007-09-07 05:26:58 +08:00
|
|
|
void VisitUnaryOperator(UnaryOperator* U);
|
2009-09-09 23:08:12 +08:00
|
|
|
void Visit(Stmt *S);
|
|
|
|
void VisitTerminator(CFGBlock* B);
|
|
|
|
|
2008-04-16 02:35:30 +08:00
|
|
|
void SetTopValue(LiveVariables::ValTy& V) {
|
2009-01-31 05:35:30 +08:00
|
|
|
V = AD.AlwaysLive;
|
2008-04-16 02:35:30 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-06 08:17:54 +08:00
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-02 04:33:52 +08:00
|
|
|
void TransferFuncs::Visit(Stmt *S) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-18 04:48:37 +08:00
|
|
|
if (S == getCurrentBlkStmt()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-04 06:25:27 +08:00
|
|
|
if (AD.Observer)
|
|
|
|
AD.Observer->ObserveStmt(S,AD,LiveState);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-06 03:26:46 +08:00
|
|
|
if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
|
2008-01-18 08:40:21 +08:00
|
|
|
StmtVisitor<TransferFuncs,void>::Visit(S);
|
2008-01-18 04:48:37 +08:00
|
|
|
}
|
2008-07-04 06:25:27 +08:00
|
|
|
else if (!getCFG().isBlkExpr(S)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-04 06:25:27 +08:00
|
|
|
if (AD.Observer)
|
|
|
|
AD.Observer->ObserveStmt(S,AD,LiveState);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-18 04:48:37 +08:00
|
|
|
StmtVisitor<TransferFuncs,void>::Visit(S);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-04 06:25:27 +08:00
|
|
|
}
|
2009-06-30 20:11:58 +08:00
|
|
|
else {
|
2008-01-18 04:48:37 +08:00
|
|
|
// For block-level expressions, mark that they are live.
|
|
|
|
LiveState(S,AD) = Alive;
|
2009-06-30 20:11:58 +08:00
|
|
|
}
|
2007-10-02 04:33:52 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
void TransferFuncs::VisitTerminator(CFGBlock* B) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-13 05:12:18 +08:00
|
|
|
const Stmt* E = B->getTerminatorCondition();
|
2008-04-17 05:10:48 +08:00
|
|
|
|
2008-04-17 01:07:59 +08:00
|
|
|
if (!E)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-17 01:07:59 +08:00
|
|
|
assert (getCFG().isBlkExpr(E));
|
|
|
|
LiveState(E, AD) = Alive;
|
2008-04-15 12:39:08 +08:00
|
|
|
}
|
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
|
2007-09-29 04:38:59 +08:00
|
|
|
LiveState(V,AD) = Alive;
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +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
|
|
|
}
|
|
|
|
|
2008-11-13 05:58:46 +08:00
|
|
|
void
|
|
|
|
TransferFuncs::BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-15 05:07:14 +08:00
|
|
|
// This is a block-level expression. Its value is 'dead' before this point.
|
|
|
|
LiveState(S, AD) = Dead;
|
|
|
|
|
2008-11-12 03:40:47 +08:00
|
|
|
// This represents a 'use' of the collection.
|
|
|
|
Visit(S->getCollection());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 03:40:47 +08:00
|
|
|
// This represents a 'kill' for the variable.
|
2008-11-12 01:42:10 +08:00
|
|
|
Stmt* Element = S->getElement();
|
2008-11-14 09:58:12 +08:00
|
|
|
DeclRefExpr* DR = 0;
|
2008-11-12 03:40:47 +08:00
|
|
|
VarDecl* VD = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 03:40:47 +08:00
|
|
|
if (DeclStmt* DS = dyn_cast<DeclStmt>(Element))
|
2009-03-28 14:33:19 +08:00
|
|
|
VD = cast<VarDecl>(DS->getSingleDecl());
|
2008-11-12 03:40:47 +08:00
|
|
|
else {
|
2009-09-09 23:08:12 +08:00
|
|
|
Expr* ElemExpr = cast<Expr>(Element)->IgnoreParens();
|
2008-11-14 09:58:12 +08:00
|
|
|
if ((DR = dyn_cast<DeclRefExpr>(ElemExpr)))
|
|
|
|
VD = cast<VarDecl>(DR->getDecl());
|
2008-11-15 05:07:14 +08:00
|
|
|
else {
|
|
|
|
Visit(ElemExpr);
|
|
|
|
return;
|
|
|
|
}
|
2008-11-12 01:42:10 +08:00
|
|
|
}
|
2008-11-12 03:40:47 +08:00
|
|
|
|
2008-11-14 09:58:12 +08:00
|
|
|
if (VD) {
|
|
|
|
LiveState(VD, AD) = Dead;
|
|
|
|
if (AD.Observer && DR) { AD.Observer->ObserverKill(DR); }
|
|
|
|
}
|
2008-11-12 01:42:10 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
|
2008-01-18 01:50:49 +08:00
|
|
|
Expr *E = U->getSubExpr();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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:
|
|
|
|
// Walk through the subexpressions, blasting through ParenExprs
|
|
|
|
// until we either find a DeclRefExpr or some non-DeclRefExpr
|
|
|
|
// expression.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
|
2008-04-15 12:08:54 +08:00
|
|
|
if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
|
|
|
|
// Treat the --/++ operator as a kill.
|
2008-02-22 08:34:10 +08:00
|
|
|
if (AD.Observer) { AD.Observer->ObserverKill(DR); }
|
2008-04-15 12:08:54 +08:00
|
|
|
LiveState(VD, AD) = Alive;
|
2008-02-22 08:34:10 +08:00
|
|
|
return VisitDeclRefExpr(DR);
|
|
|
|
}
|
2007-09-29 05:29:33 +08:00
|
|
|
|
|
|
|
// Fall-through.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
default:
|
2008-01-18 01:50:49 +08:00
|
|
|
return Visit(E);
|
2007-09-07 05:26:58 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
void TransferFuncs::VisitAssign(BinaryOperator* B) {
|
2008-01-18 01:50:49 +08:00
|
|
|
Expr* LHS = B->getLHS();
|
2007-09-25 12:31:27 +08:00
|
|
|
|
2007-09-29 05:29:33 +08:00
|
|
|
// Assigning to a variable?
|
2008-01-18 01:50:49 +08:00
|
|
|
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 02:35:30 +08:00
|
|
|
// Update liveness inforamtion.
|
|
|
|
unsigned bit = AD.getIdx(DR->getDecl());
|
|
|
|
LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-29 04:38:59 +08:00
|
|
|
if (AD.Observer) { AD.Observer->ObserverKill(DR); }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
// Handle things like +=, etc., which also generate "uses"
|
|
|
|
// of a variable. Do this just by visiting the subexpression.
|
2007-09-29 05:29:33 +08:00
|
|
|
if (B->getOpcode() != BinaryOperator::Assign)
|
|
|
|
VisitDeclRefExpr(DR);
|
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);
|
2009-09-09 23:08:12 +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.
|
2008-08-06 04:46:55 +08:00
|
|
|
for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE = DS->decl_end();
|
|
|
|
DI != DE; ++DI)
|
|
|
|
if (VarDecl* VD = dyn_cast<VarDecl>(*DI)) {
|
2008-09-26 13:52:45 +08:00
|
|
|
// The initializer is evaluated after the variable comes into scope.
|
|
|
|
// Since this is a reverse dataflow analysis, we must evaluate the
|
|
|
|
// transfer function for this expression first.
|
|
|
|
if (Expr* Init = VD->getInit())
|
|
|
|
Visit(Init);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-09 08:14:14 +08:00
|
|
|
if (const VariableArrayType* VT =
|
|
|
|
AD.getContext().getAsVariableArrayType(VD->getType())) {
|
|
|
|
StmtIterator I(const_cast<VariableArrayType*>(VT));
|
2009-09-09 23:08:12 +08:00
|
|
|
StmtIterator E;
|
2008-12-09 08:14:14 +08:00
|
|
|
for (; I != E; ++I) Visit(*I);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-26 13:52:45 +08:00
|
|
|
// Update liveness information by killing the VarDecl.
|
2008-04-16 02:35:30 +08:00
|
|
|
unsigned bit = AD.getIdx(VD);
|
|
|
|
LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
|
2008-02-26 06:28:54 +08:00
|
|
|
}
|
2007-09-29 04:38:59 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
} // end anonymous namespace
|
2007-09-06 08:17:54 +08:00
|
|
|
|
2007-09-29 04:38:59 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Merge operator: if something is live on any successor block, it is live
|
|
|
|
// in the current block (a set union).
|
2009-09-09 23:08:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-06 08:17:54 +08:00
|
|
|
|
2007-09-25 12:31:27 +08:00
|
|
|
namespace {
|
2008-03-21 05:46:49 +08:00
|
|
|
|
|
|
|
struct Merge {
|
2009-09-09 23:08:12 +08:00
|
|
|
typedef StmtDeclBitVector_Types::ValTy ValTy;
|
|
|
|
|
2008-03-21 05:46:49 +08:00
|
|
|
void operator()(ValTy& Dst, const ValTy& Src) {
|
|
|
|
Dst.OrDeclBits(Src);
|
2009-01-31 05:35:30 +08:00
|
|
|
Dst.OrBlkExprBits(Src);
|
2008-03-21 05:46:49 +08:00
|
|
|
}
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-21 05:46:49 +08:00
|
|
|
typedef DataflowSolver<LiveVariables, TransferFuncs, Merge> Solver;
|
2007-09-29 04:38:59 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// External interface to run Liveness analysis.
|
2009-09-09 23:08:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-06 08:17:54 +08:00
|
|
|
|
2008-01-11 08:40:29 +08:00
|
|
|
void LiveVariables::runOnCFG(CFG& cfg) {
|
2007-09-25 12:31:27 +08:00
|
|
|
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,
|
2008-01-18 02:25:22 +08:00
|
|
|
LiveVariables::ObserverTy* Obs,
|
|
|
|
bool recordStmtValues) {
|
2007-09-25 12:31:27 +08:00
|
|
|
Solver S(*this);
|
|
|
|
ObserverTy* OldObserver = getAnalysisData().Observer;
|
2008-01-18 02:25:22 +08:00
|
|
|
getAnalysisData().Observer = Obs;
|
|
|
|
S.runOnAllBlocks(cfg, recordStmtValues);
|
2007-09-25 12:31:27 +08:00
|
|
|
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 {
|
2008-03-14 00:55:07 +08:00
|
|
|
DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
|
|
|
|
return i.isValid() ? getBlockData(B).getBit(i) : false;
|
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 {
|
2008-03-14 00:55:07 +08:00
|
|
|
DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
|
|
|
|
return i.isValid() ? Live.getBit(i) : false;
|
2007-09-07 07:00:42 +08:00
|
|
|
}
|
|
|
|
|
2008-01-18 04:48:37 +08:00
|
|
|
bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
|
|
|
|
return getStmtData(Loc)(StmtVal,getAnalysisData());
|
|
|
|
}
|
|
|
|
|
2008-01-18 08:40:21 +08:00
|
|
|
bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
|
|
|
|
return getStmtData(Loc)(D,getAnalysisData());
|
|
|
|
}
|
|
|
|
|
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();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-29 04:38:59 +08:00
|
|
|
for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
|
|
|
|
E = AD.end_decl(); I!=E; ++I)
|
2009-09-09 23:08:12 +08:00
|
|
|
if (V.getDeclBit(I->second)) {
|
2009-01-27 15:57:44 +08:00
|
|
|
fprintf(stderr, " %s <", I->first->getIdentifier()->getName());
|
|
|
|
I->first->getLocation().dump(SM);
|
|
|
|
fprintf(stderr, ">\n");
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
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());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-07 05:26:58 +08:00
|
|
|
dumpLiveness(I->second,M);
|
|
|
|
}
|
2007-09-11 01:36:42 +08:00
|
|
|
|
|
|
|
fprintf(stderr,"\n");
|
|
|
|
}
|