blob: 96c9e69d1af04d489719dfc35754a9e35ebb3fae [file] [log] [blame]
Ted Kremenek24c62442007-09-20 21:42:55 +00001//=- LiveVariables.cpp - Live Variable Analysis for Source CFGs -*- C++ --*-==//
Ted Kremenekb56a9902007-09-06 00:17:54 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenekb56a9902007-09-06 00:17:54 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Live Variables analysis for source-level CFGs.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekbf593f82007-12-21 21:42:19 +000014#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek3f8ed262007-09-06 21:26:58 +000015#include "clang/Basic/SourceManager.h"
Ted Kremenek6ee0a112008-12-09 00:14:14 +000016#include "clang/AST/ASTContext.h"
Ted Kremenekb56a9902007-09-06 00:17:54 +000017#include "clang/AST/Expr.h"
Ted Kremenek6796fbd2009-07-16 18:13:04 +000018#include "clang/Analysis/CFG.h"
Ted Kremenekad8bce02007-09-25 04:31:27 +000019#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenek39fc60f2007-09-25 21:00:24 +000020#include "clang/Analysis/FlowSensitive/DataflowSolver.h"
Ted Kremenekb56a9902007-09-06 00:17:54 +000021#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek82ff6d62008-04-15 18:35:30 +000022#include "llvm/ADT/SmallVector.h"
Ted Kremenek96b1ce42008-01-08 18:19:08 +000023#include "llvm/Support/Compiler.h"
Daniel Dunbare81a5532009-10-17 18:12:37 +000024#include "llvm/Support/raw_ostream.h"
Ted Kremenekb56a9902007-09-06 00:17:54 +000025
26using namespace clang;
27
28//===----------------------------------------------------------------------===//
Ted Kremenek82ff6d62008-04-15 18:35:30 +000029// Useful constants.
Mike Stump11289f42009-09-09 15:08:12 +000030//===----------------------------------------------------------------------===//
Ted Kremenek82ff6d62008-04-15 18:35:30 +000031
32static const bool Alive = true;
Mike Stump11289f42009-09-09 15:08:12 +000033static const bool Dead = false;
Ted Kremenek82ff6d62008-04-15 18:35:30 +000034
35//===----------------------------------------------------------------------===//
Ted Kremenekad8bce02007-09-25 04:31:27 +000036// Dataflow initialization logic.
Mike Stump11289f42009-09-09 15:08:12 +000037//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +000038
39namespace {
Mike Stump11289f42009-09-09 15:08:12 +000040class VISIBILITY_HIDDEN RegisterDecls
Ted Kremenek96b1ce42008-01-08 18:19:08 +000041 : public CFGRecStmtDeclVisitor<RegisterDecls> {
Mike Stump11289f42009-09-09 15:08:12 +000042
Ted Kremenekad8bce02007-09-25 04:31:27 +000043 LiveVariables::AnalysisDataTy& AD;
Mike Stump11289f42009-09-09 15:08:12 +000044
Ted Kremenek82ff6d62008-04-15 18:35:30 +000045 typedef llvm::SmallVector<VarDecl*, 20> AlwaysLiveTy;
46 AlwaysLiveTy AlwaysLive;
47
Mike Stump11289f42009-09-09 15:08:12 +000048
Ted Kremenek0064ff42007-09-28 20:38:59 +000049public:
Ted Kremenek82ff6d62008-04-15 18:35:30 +000050 RegisterDecls(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
51
52 ~RegisterDecls() {
53
54 AD.AlwaysLive.resetValues(AD);
Mike Stump11289f42009-09-09 15:08:12 +000055
Ted Kremenek82ff6d62008-04-15 18:35:30 +000056 for (AlwaysLiveTy::iterator I = AlwaysLive.begin(), E = AlwaysLive.end();
Mike Stump11289f42009-09-09 15:08:12 +000057 I != E; ++ I)
58 AD.AlwaysLive(*I, AD) = Alive;
Ted Kremenek82ff6d62008-04-15 18:35:30 +000059 }
60
Chris Lattner5696e7b2008-06-17 18:05:57 +000061 void VisitImplicitParamDecl(ImplicitParamDecl* IPD) {
62 // Register the VarDecl for tracking.
63 AD.Register(IPD);
64 }
65
Ted Kremenek82ff6d62008-04-15 18:35:30 +000066 void VisitVarDecl(VarDecl* VD) {
67 // Register the VarDecl for tracking.
68 AD.Register(VD);
Mike Stump11289f42009-09-09 15:08:12 +000069
Ted Kremenek82ff6d62008-04-15 18:35:30 +000070 // Does the variable have global storage? If so, it is always live.
71 if (VD->hasGlobalStorage())
Mike Stump11289f42009-09-09 15:08:12 +000072 AlwaysLive.push_back(VD);
Ted Kremenek82ff6d62008-04-15 18:35:30 +000073 }
Mike Stump11289f42009-09-09 15:08:12 +000074
Ted Kremenek9d0acca2007-11-20 03:01:58 +000075 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenekfb4750b2007-10-01 20:33:52 +000076};
Ted Kremenekb56a9902007-09-06 00:17:54 +000077} // end anonymous namespace
78
Ted Kremenek6ee0a112008-12-09 00:14:14 +000079LiveVariables::LiveVariables(ASTContext& Ctx, CFG& cfg) {
Ted Kremenek1fdd0a42008-03-13 16:55:07 +000080 // Register all referenced VarDecls.
Ted Kremenek6ee0a112008-12-09 00:14:14 +000081 getAnalysisData().setCFG(cfg);
82 getAnalysisData().setContext(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +000083
Ted Kremenekfb4750b2007-10-01 20:33:52 +000084 RegisterDecls R(getAnalysisData());
Ted Kremenekad8bce02007-09-25 04:31:27 +000085 cfg.VisitBlockStmts(R);
86}
Ted Kremenekb56a9902007-09-06 00:17:54 +000087
88//===----------------------------------------------------------------------===//
Ted Kremenekad8bce02007-09-25 04:31:27 +000089// Transfer functions.
Mike Stump11289f42009-09-09 15:08:12 +000090//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +000091
92namespace {
Ted Kremenek0064ff42007-09-28 20:38:59 +000093
Ted Kremenek96b1ce42008-01-08 18:19:08 +000094class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekad8bce02007-09-25 04:31:27 +000095 LiveVariables::AnalysisDataTy& AD;
Ted Kremenek0064ff42007-09-28 20:38:59 +000096 LiveVariables::ValTy LiveState;
Ted Kremenekb56a9902007-09-06 00:17:54 +000097public:
Ted Kremenekfb4750b2007-10-01 20:33:52 +000098 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekad8bce02007-09-25 04:31:27 +000099
Ted Kremenek0064ff42007-09-28 20:38:59 +0000100 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek9d0acca2007-11-20 03:01:58 +0000101 CFG& getCFG() { return AD.getCFG(); }
Mike Stump11289f42009-09-09 15:08:12 +0000102
Ted Kremenekb56a9902007-09-06 00:17:54 +0000103 void VisitDeclRefExpr(DeclRefExpr* DR);
104 void VisitBinaryOperator(BinaryOperator* B);
105 void VisitAssign(BinaryOperator* B);
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000106 void VisitDeclStmt(DeclStmt* DS);
Ted Kremenek65dd30f2008-11-12 21:58:46 +0000107 void BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S);
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000108 void VisitUnaryOperator(UnaryOperator* U);
Mike Stump11289f42009-09-09 15:08:12 +0000109 void Visit(Stmt *S);
110 void VisitTerminator(CFGBlock* B);
111
Ted Kremenek82ff6d62008-04-15 18:35:30 +0000112 void SetTopValue(LiveVariables::ValTy& V) {
Ted Kremenek378e7fd2009-01-30 21:35:30 +0000113 V = AD.AlwaysLive;
Ted Kremenek82ff6d62008-04-15 18:35:30 +0000114 }
Mike Stump11289f42009-09-09 15:08:12 +0000115
Ted Kremenekb56a9902007-09-06 00:17:54 +0000116};
Mike Stump11289f42009-09-09 15:08:12 +0000117
Ted Kremenekfb4750b2007-10-01 20:33:52 +0000118void TransferFuncs::Visit(Stmt *S) {
Mike Stump11289f42009-09-09 15:08:12 +0000119
Ted Kremenek85be7cf2008-01-17 20:48:37 +0000120 if (S == getCurrentBlkStmt()) {
Mike Stump11289f42009-09-09 15:08:12 +0000121
Ted Kremenek80440462008-07-03 22:25:27 +0000122 if (AD.Observer)
123 AD.Observer->ObserveStmt(S,AD,LiveState);
Mike Stump11289f42009-09-09 15:08:12 +0000124
Ted Kremenek0c046062008-03-05 19:26:46 +0000125 if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
Ted Kremenek05ecfdd2008-01-18 00:40:21 +0000126 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenek85be7cf2008-01-17 20:48:37 +0000127 }
Ted Kremenek80440462008-07-03 22:25:27 +0000128 else if (!getCFG().isBlkExpr(S)) {
Mike Stump11289f42009-09-09 15:08:12 +0000129
Ted Kremenek80440462008-07-03 22:25:27 +0000130 if (AD.Observer)
131 AD.Observer->ObserveStmt(S,AD,LiveState);
Mike Stump11289f42009-09-09 15:08:12 +0000132
Ted Kremenek85be7cf2008-01-17 20:48:37 +0000133 StmtVisitor<TransferFuncs,void>::Visit(S);
Mike Stump11289f42009-09-09 15:08:12 +0000134
Ted Kremenek80440462008-07-03 22:25:27 +0000135 }
Zhongxing Xud29e74e2009-06-30 12:11:58 +0000136 else {
Ted Kremenek85be7cf2008-01-17 20:48:37 +0000137 // For block-level expressions, mark that they are live.
138 LiveState(S,AD) = Alive;
Zhongxing Xud29e74e2009-06-30 12:11:58 +0000139 }
Ted Kremenekfb4750b2007-10-01 20:33:52 +0000140}
Mike Stump11289f42009-09-09 15:08:12 +0000141
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000142void TransferFuncs::VisitTerminator(CFGBlock* B) {
Mike Stump11289f42009-09-09 15:08:12 +0000143
Ted Kremenek97450fe2008-11-12 21:12:18 +0000144 const Stmt* E = B->getTerminatorCondition();
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000145
Ted Kremeneked30e8d2008-04-16 17:07:59 +0000146 if (!E)
147 return;
Mike Stump11289f42009-09-09 15:08:12 +0000148
Ted Kremeneked30e8d2008-04-16 17:07:59 +0000149 assert (getCFG().isBlkExpr(E));
150 LiveState(E, AD) = Alive;
Ted Kremenek8adeebb2008-04-15 04:39:08 +0000151}
152
Ted Kremenekad8bce02007-09-25 04:31:27 +0000153void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
Mike Stump11289f42009-09-09 15:08:12 +0000154 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenek0064ff42007-09-28 20:38:59 +0000155 LiveState(V,AD) = Alive;
Ted Kremenekb56a9902007-09-06 00:17:54 +0000156}
Mike Stump11289f42009-09-09 15:08:12 +0000157
158void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek1147e362007-09-12 19:10:52 +0000159 if (B->isAssignmentOp()) VisitAssign(B);
160 else VisitStmt(B);
Ted Kremenekb56a9902007-09-06 00:17:54 +0000161}
162
Ted Kremenek65dd30f2008-11-12 21:58:46 +0000163void
164TransferFuncs::BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
Mike Stump11289f42009-09-09 15:08:12 +0000165
Ted Kremenekfc419a02008-11-14 21:07:14 +0000166 // This is a block-level expression. Its value is 'dead' before this point.
167 LiveState(S, AD) = Dead;
168
Ted Kremenek3b4e1d52008-11-11 19:40:47 +0000169 // This represents a 'use' of the collection.
170 Visit(S->getCollection());
Mike Stump11289f42009-09-09 15:08:12 +0000171
Ted Kremenekfbd2f402008-11-11 17:42:10 +0000172 // This represents a 'kill' for the variable.
Ted Kremenek3b4e1d52008-11-11 19:40:47 +0000173 Stmt* Element = S->getElement();
Ted Kremenek99d4ff32008-11-14 01:58:12 +0000174 DeclRefExpr* DR = 0;
Ted Kremenek3b4e1d52008-11-11 19:40:47 +0000175 VarDecl* VD = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000176
Ted Kremenek3b4e1d52008-11-11 19:40:47 +0000177 if (DeclStmt* DS = dyn_cast<DeclStmt>(Element))
Chris Lattner529efc72009-03-28 06:33:19 +0000178 VD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenek3b4e1d52008-11-11 19:40:47 +0000179 else {
Mike Stump11289f42009-09-09 15:08:12 +0000180 Expr* ElemExpr = cast<Expr>(Element)->IgnoreParens();
Ted Kremenek99d4ff32008-11-14 01:58:12 +0000181 if ((DR = dyn_cast<DeclRefExpr>(ElemExpr)))
182 VD = cast<VarDecl>(DR->getDecl());
Ted Kremenekfc419a02008-11-14 21:07:14 +0000183 else {
184 Visit(ElemExpr);
185 return;
186 }
Ted Kremenek3b4e1d52008-11-11 19:40:47 +0000187 }
188
Ted Kremenek99d4ff32008-11-14 01:58:12 +0000189 if (VD) {
190 LiveState(VD, AD) = Dead;
191 if (AD.Observer && DR) { AD.Observer->ObserverKill(DR); }
192 }
Ted Kremenekfbd2f402008-11-11 17:42:10 +0000193}
194
Mike Stump11289f42009-09-09 15:08:12 +0000195
Ted Kremenekad8bce02007-09-25 04:31:27 +0000196void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenekf1dae232008-01-17 17:50:49 +0000197 Expr *E = U->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000198
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000199 switch (U->getOpcode()) {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000200 case UnaryOperator::PostInc:
201 case UnaryOperator::PostDec:
202 case UnaryOperator::PreInc:
203 case UnaryOperator::PreDec:
Ted Kremenekad8bce02007-09-25 04:31:27 +0000204 // Walk through the subexpressions, blasting through ParenExprs
205 // until we either find a DeclRefExpr or some non-DeclRefExpr
206 // expression.
Mike Stump11289f42009-09-09 15:08:12 +0000207 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
Ted Kremenekcd76f952008-04-15 04:08:54 +0000208 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
209 // Treat the --/++ operator as a kill.
Ted Kremenek20c91422008-02-22 00:34:10 +0000210 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
Ted Kremenekcd76f952008-04-15 04:08:54 +0000211 LiveState(VD, AD) = Alive;
Ted Kremenek20c91422008-02-22 00:34:10 +0000212 return VisitDeclRefExpr(DR);
213 }
Ted Kremenek14851c32007-09-28 21:29:33 +0000214
215 // Fall-through.
Mike Stump11289f42009-09-09 15:08:12 +0000216
Ted Kremenekad8bce02007-09-25 04:31:27 +0000217 default:
Ted Kremenekf1dae232008-01-17 17:50:49 +0000218 return Visit(E);
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000219 }
220}
Mike Stump11289f42009-09-09 15:08:12 +0000221
222void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenekf1dae232008-01-17 17:50:49 +0000223 Expr* LHS = B->getLHS();
Ted Kremenekad8bce02007-09-25 04:31:27 +0000224
Ted Kremenek14851c32007-09-28 21:29:33 +0000225 // Assigning to a variable?
Ted Kremenekf1dae232008-01-17 17:50:49 +0000226 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Mike Stump11289f42009-09-09 15:08:12 +0000227
Ted Kremenek82ff6d62008-04-15 18:35:30 +0000228 // Update liveness inforamtion.
229 unsigned bit = AD.getIdx(DR->getDecl());
230 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
Mike Stump11289f42009-09-09 15:08:12 +0000231
Ted Kremenek0064ff42007-09-28 20:38:59 +0000232 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
Mike Stump11289f42009-09-09 15:08:12 +0000233
Ted Kremenekad8bce02007-09-25 04:31:27 +0000234 // Handle things like +=, etc., which also generate "uses"
235 // of a variable. Do this just by visiting the subexpression.
Ted Kremenek14851c32007-09-28 21:29:33 +0000236 if (B->getOpcode() != BinaryOperator::Assign)
237 VisitDeclRefExpr(DR);
Ted Kremenekb56a9902007-09-06 00:17:54 +0000238 }
Ted Kremenekad8bce02007-09-25 04:31:27 +0000239 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000240 Visit(LHS);
Mike Stump11289f42009-09-09 15:08:12 +0000241
Ted Kremenekad8bce02007-09-25 04:31:27 +0000242 Visit(B->getRHS());
Ted Kremenekb56a9902007-09-06 00:17:54 +0000243}
244
Ted Kremenekad8bce02007-09-25 04:31:27 +0000245void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
246 // Declarations effectively "kill" a variable since they cannot
247 // possibly be live before they are declared.
Ted Kremenek4f8792b2008-08-05 20:46:55 +0000248 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE = DS->decl_end();
249 DI != DE; ++DI)
250 if (VarDecl* VD = dyn_cast<VarDecl>(*DI)) {
Ted Kremenek2ece64b2008-09-26 05:52:45 +0000251 // The initializer is evaluated after the variable comes into scope.
252 // Since this is a reverse dataflow analysis, we must evaluate the
253 // transfer function for this expression first.
Ted Kremeneka56c08a2008-02-07 02:38:55 +0000254 if (Expr* Init = VD->getInit())
255 Visit(Init);
Mike Stump11289f42009-09-09 15:08:12 +0000256
Ted Kremenek6ee0a112008-12-09 00:14:14 +0000257 if (const VariableArrayType* VT =
258 AD.getContext().getAsVariableArrayType(VD->getType())) {
259 StmtIterator I(const_cast<VariableArrayType*>(VT));
Mike Stump11289f42009-09-09 15:08:12 +0000260 StmtIterator E;
Ted Kremenek6ee0a112008-12-09 00:14:14 +0000261 for (; I != E; ++I) Visit(*I);
262 }
Mike Stump11289f42009-09-09 15:08:12 +0000263
Ted Kremenek2ece64b2008-09-26 05:52:45 +0000264 // Update liveness information by killing the VarDecl.
265 unsigned bit = AD.getIdx(VD);
266 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
Ted Kremenek7845b262008-02-25 22:28:54 +0000267 }
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000268}
Mike Stump11289f42009-09-09 15:08:12 +0000269
Ted Kremenekb56a9902007-09-06 00:17:54 +0000270} // end anonymous namespace
271
Ted Kremenek0064ff42007-09-28 20:38:59 +0000272//===----------------------------------------------------------------------===//
273// Merge operator: if something is live on any successor block, it is live
274// in the current block (a set union).
Mike Stump11289f42009-09-09 15:08:12 +0000275//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +0000276
Ted Kremenekad8bce02007-09-25 04:31:27 +0000277namespace {
Ted Kremenekb7151c72008-03-20 21:46:49 +0000278
279struct Merge {
Mike Stump11289f42009-09-09 15:08:12 +0000280 typedef StmtDeclBitVector_Types::ValTy ValTy;
281
Ted Kremenekb7151c72008-03-20 21:46:49 +0000282 void operator()(ValTy& Dst, const ValTy& Src) {
283 Dst.OrDeclBits(Src);
Ted Kremenek378e7fd2009-01-30 21:35:30 +0000284 Dst.OrBlkExprBits(Src);
Ted Kremenekb7151c72008-03-20 21:46:49 +0000285 }
286};
Mike Stump11289f42009-09-09 15:08:12 +0000287
Ted Kremenekb7151c72008-03-20 21:46:49 +0000288typedef DataflowSolver<LiveVariables, TransferFuncs, Merge> Solver;
Ted Kremenek0064ff42007-09-28 20:38:59 +0000289} // end anonymous namespace
290
291//===----------------------------------------------------------------------===//
292// External interface to run Liveness analysis.
Mike Stump11289f42009-09-09 15:08:12 +0000293//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +0000294
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +0000295void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000296 Solver S(*this);
297 S.runOnCFG(cfg);
298}
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000299
Ted Kremenekad8bce02007-09-25 04:31:27 +0000300void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenekb4b65e62008-01-17 18:25:22 +0000301 LiveVariables::ObserverTy* Obs,
302 bool recordStmtValues) {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000303 Solver S(*this);
304 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenekb4b65e62008-01-17 18:25:22 +0000305 getAnalysisData().Observer = Obs;
306 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekad8bce02007-09-25 04:31:27 +0000307 getAnalysisData().Observer = OldObserver;
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000308}
309
310//===----------------------------------------------------------------------===//
311// liveness queries
312//
313
Ted Kremenekbd9cc5c2007-09-10 17:36:42 +0000314bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenek1fdd0a42008-03-13 16:55:07 +0000315 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
316 return i.isValid() ? getBlockData(B).getBit(i) : false;
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000317}
318
Ted Kremenekad8bce02007-09-25 04:31:27 +0000319bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenek1fdd0a42008-03-13 16:55:07 +0000320 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
321 return i.isValid() ? Live.getBit(i) : false;
Ted Kremenek6dc7b112007-09-06 23:00:42 +0000322}
323
Ted Kremenek85be7cf2008-01-17 20:48:37 +0000324bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
325 return getStmtData(Loc)(StmtVal,getAnalysisData());
326}
327
Ted Kremenek05ecfdd2008-01-18 00:40:21 +0000328bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
329 return getStmtData(Loc)(D,getAnalysisData());
330}
331
Ted Kremenek6dc7b112007-09-06 23:00:42 +0000332//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +0000333// printing liveness state for debugging
334//
335
Ted Kremenekad8bce02007-09-25 04:31:27 +0000336void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
337 const AnalysisDataTy& AD = getAnalysisData();
Mike Stump11289f42009-09-09 15:08:12 +0000338
Ted Kremenek0064ff42007-09-28 20:38:59 +0000339 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
340 E = AD.end_decl(); I!=E; ++I)
Mike Stump11289f42009-09-09 15:08:12 +0000341 if (V.getDeclBit(I->second)) {
Daniel Dunbare81a5532009-10-17 18:12:37 +0000342 llvm::errs() << " " << I->first->getIdentifier()->getNameStr() << " <";
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000343 I->first->getLocation().dump(SM);
Daniel Dunbare81a5532009-10-17 18:12:37 +0000344 llvm::errs() << ">\n";
Ted Kremenekb56a9902007-09-06 00:17:54 +0000345 }
Mike Stump11289f42009-09-09 15:08:12 +0000346}
Ted Kremenekb56a9902007-09-06 00:17:54 +0000347
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000348void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000349 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
350 E = getBlockDataMap().end(); I!=E; ++I) {
Daniel Dunbare81a5532009-10-17 18:12:37 +0000351 llvm::errs() << "\n[ B" << I->first->getBlockID()
352 << " (live variables at block exit) ]\n";
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000353 dumpLiveness(I->second,M);
354 }
Ted Kremenekbd9cc5c2007-09-10 17:36:42 +0000355
Daniel Dunbare81a5532009-10-17 18:12:37 +0000356 llvm::errs() << "\n";
Ted Kremenekbd9cc5c2007-09-10 17:36:42 +0000357}