blob: e61f27bd39a7c88e93ec5b9cf358e39c3f2b1143 [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 Kremenek975a1192009-11-07 05:57:35 +000021#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremenek0f5e6f882009-11-26 02:31:33 +000022#include "clang/Analysis/PathSensitive/AnalysisContext.h"
Ted Kremenekb56a9902007-09-06 00:17:54 +000023#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek82ff6d62008-04-15 18:35:30 +000024#include "llvm/ADT/SmallVector.h"
Ted Kremenek96b1ce42008-01-08 18:19:08 +000025#include "llvm/Support/Compiler.h"
Daniel Dunbare81a5532009-10-17 18:12:37 +000026#include "llvm/Support/raw_ostream.h"
Ted Kremenekb56a9902007-09-06 00:17:54 +000027
28using namespace clang;
29
30//===----------------------------------------------------------------------===//
Ted Kremenek82ff6d62008-04-15 18:35:30 +000031// Useful constants.
Mike Stump11289f42009-09-09 15:08:12 +000032//===----------------------------------------------------------------------===//
Ted Kremenek82ff6d62008-04-15 18:35:30 +000033
34static const bool Alive = true;
Mike Stump11289f42009-09-09 15:08:12 +000035static const bool Dead = false;
Ted Kremenek82ff6d62008-04-15 18:35:30 +000036
37//===----------------------------------------------------------------------===//
Ted Kremenekad8bce02007-09-25 04:31:27 +000038// Dataflow initialization logic.
Mike Stump11289f42009-09-09 15:08:12 +000039//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +000040
41namespace {
Mike Stump11289f42009-09-09 15:08:12 +000042class VISIBILITY_HIDDEN RegisterDecls
Ted Kremenek96b1ce42008-01-08 18:19:08 +000043 : public CFGRecStmtDeclVisitor<RegisterDecls> {
Mike Stump11289f42009-09-09 15:08:12 +000044
Ted Kremenekad8bce02007-09-25 04:31:27 +000045 LiveVariables::AnalysisDataTy& AD;
Mike Stump11289f42009-09-09 15:08:12 +000046
Ted Kremenek82ff6d62008-04-15 18:35:30 +000047 typedef llvm::SmallVector<VarDecl*, 20> AlwaysLiveTy;
48 AlwaysLiveTy AlwaysLive;
49
Mike Stump11289f42009-09-09 15:08:12 +000050
Ted Kremenek0064ff42007-09-28 20:38:59 +000051public:
Ted Kremenek82ff6d62008-04-15 18:35:30 +000052 RegisterDecls(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
53
54 ~RegisterDecls() {
55
56 AD.AlwaysLive.resetValues(AD);
Mike Stump11289f42009-09-09 15:08:12 +000057
Ted Kremenek82ff6d62008-04-15 18:35:30 +000058 for (AlwaysLiveTy::iterator I = AlwaysLive.begin(), E = AlwaysLive.end();
Mike Stump11289f42009-09-09 15:08:12 +000059 I != E; ++ I)
60 AD.AlwaysLive(*I, AD) = Alive;
Ted Kremenek82ff6d62008-04-15 18:35:30 +000061 }
62
Chris Lattner5696e7b2008-06-17 18:05:57 +000063 void VisitImplicitParamDecl(ImplicitParamDecl* IPD) {
64 // Register the VarDecl for tracking.
65 AD.Register(IPD);
66 }
67
Ted Kremenek82ff6d62008-04-15 18:35:30 +000068 void VisitVarDecl(VarDecl* VD) {
69 // Register the VarDecl for tracking.
70 AD.Register(VD);
Mike Stump11289f42009-09-09 15:08:12 +000071
Ted Kremenek82ff6d62008-04-15 18:35:30 +000072 // Does the variable have global storage? If so, it is always live.
73 if (VD->hasGlobalStorage())
Mike Stump11289f42009-09-09 15:08:12 +000074 AlwaysLive.push_back(VD);
Ted Kremenek82ff6d62008-04-15 18:35:30 +000075 }
Mike Stump11289f42009-09-09 15:08:12 +000076
Ted Kremenek9d0acca2007-11-20 03:01:58 +000077 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenekfb4750b2007-10-01 20:33:52 +000078};
Ted Kremenekb56a9902007-09-06 00:17:54 +000079} // end anonymous namespace
80
Ted Kremenek0f5e6f882009-11-26 02:31:33 +000081LiveVariables::LiveVariables(AnalysisContext &AC) {
Ted Kremenek1fdd0a42008-03-13 16:55:07 +000082 // Register all referenced VarDecls.
Ted Kremenek0f5e6f882009-11-26 02:31:33 +000083 CFG &cfg = *AC.getCFG();
Ted Kremenek6ee0a112008-12-09 00:14:14 +000084 getAnalysisData().setCFG(cfg);
Ted Kremenek0f5e6f882009-11-26 02:31:33 +000085 getAnalysisData().setContext(AC.getASTContext());
86 getAnalysisData().AC = &AC;
Mike Stump11289f42009-09-09 15:08:12 +000087
Ted Kremenekfb4750b2007-10-01 20:33:52 +000088 RegisterDecls R(getAnalysisData());
Ted Kremenekad8bce02007-09-25 04:31:27 +000089 cfg.VisitBlockStmts(R);
90}
Ted Kremenekb56a9902007-09-06 00:17:54 +000091
92//===----------------------------------------------------------------------===//
Ted Kremenekad8bce02007-09-25 04:31:27 +000093// Transfer functions.
Mike Stump11289f42009-09-09 15:08:12 +000094//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +000095
96namespace {
Ted Kremenek0064ff42007-09-28 20:38:59 +000097
Ted Kremenek96b1ce42008-01-08 18:19:08 +000098class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekad8bce02007-09-25 04:31:27 +000099 LiveVariables::AnalysisDataTy& AD;
Ted Kremenek0064ff42007-09-28 20:38:59 +0000100 LiveVariables::ValTy LiveState;
Ted Kremenekb56a9902007-09-06 00:17:54 +0000101public:
Ted Kremenekfb4750b2007-10-01 20:33:52 +0000102 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekad8bce02007-09-25 04:31:27 +0000103
Ted Kremenek0064ff42007-09-28 20:38:59 +0000104 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek9d0acca2007-11-20 03:01:58 +0000105 CFG& getCFG() { return AD.getCFG(); }
Mike Stump11289f42009-09-09 15:08:12 +0000106
Ted Kremenekb56a9902007-09-06 00:17:54 +0000107 void VisitDeclRefExpr(DeclRefExpr* DR);
108 void VisitBinaryOperator(BinaryOperator* B);
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000109 void VisitBlockExpr(BlockExpr *B);
Ted Kremenekb56a9902007-09-06 00:17:54 +0000110 void VisitAssign(BinaryOperator* B);
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000111 void VisitDeclStmt(DeclStmt* DS);
Ted Kremenek65dd30f2008-11-12 21:58:46 +0000112 void BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S);
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000113 void VisitUnaryOperator(UnaryOperator* U);
Mike Stump11289f42009-09-09 15:08:12 +0000114 void Visit(Stmt *S);
115 void VisitTerminator(CFGBlock* B);
116
Ted Kremenek82ff6d62008-04-15 18:35:30 +0000117 void SetTopValue(LiveVariables::ValTy& V) {
Ted Kremenek378e7fd2009-01-30 21:35:30 +0000118 V = AD.AlwaysLive;
Ted Kremenek82ff6d62008-04-15 18:35:30 +0000119 }
Mike Stump11289f42009-09-09 15:08:12 +0000120
Ted Kremenekb56a9902007-09-06 00:17:54 +0000121};
Mike Stump11289f42009-09-09 15:08:12 +0000122
Ted Kremenekfb4750b2007-10-01 20:33:52 +0000123void TransferFuncs::Visit(Stmt *S) {
Mike Stump11289f42009-09-09 15:08:12 +0000124
Ted Kremenek85be7cf2008-01-17 20:48:37 +0000125 if (S == getCurrentBlkStmt()) {
Mike Stump11289f42009-09-09 15:08:12 +0000126
Ted Kremenek80440462008-07-03 22:25:27 +0000127 if (AD.Observer)
128 AD.Observer->ObserveStmt(S,AD,LiveState);
Mike Stump11289f42009-09-09 15:08:12 +0000129
Ted Kremenek0c046062008-03-05 19:26:46 +0000130 if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
Ted Kremenek05ecfdd2008-01-18 00:40:21 +0000131 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenek85be7cf2008-01-17 20:48:37 +0000132 }
Ted Kremenek80440462008-07-03 22:25:27 +0000133 else if (!getCFG().isBlkExpr(S)) {
Mike Stump11289f42009-09-09 15:08:12 +0000134
Ted Kremenek80440462008-07-03 22:25:27 +0000135 if (AD.Observer)
136 AD.Observer->ObserveStmt(S,AD,LiveState);
Mike Stump11289f42009-09-09 15:08:12 +0000137
Ted Kremenek85be7cf2008-01-17 20:48:37 +0000138 StmtVisitor<TransferFuncs,void>::Visit(S);
Mike Stump11289f42009-09-09 15:08:12 +0000139
Ted Kremenek80440462008-07-03 22:25:27 +0000140 }
Zhongxing Xud29e74e2009-06-30 12:11:58 +0000141 else {
Ted Kremenek85be7cf2008-01-17 20:48:37 +0000142 // For block-level expressions, mark that they are live.
143 LiveState(S,AD) = Alive;
Zhongxing Xud29e74e2009-06-30 12:11:58 +0000144 }
Ted Kremenekfb4750b2007-10-01 20:33:52 +0000145}
Mike Stump11289f42009-09-09 15:08:12 +0000146
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000147void TransferFuncs::VisitTerminator(CFGBlock* B) {
Mike Stump11289f42009-09-09 15:08:12 +0000148
Ted Kremenek97450fe2008-11-12 21:12:18 +0000149 const Stmt* E = B->getTerminatorCondition();
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000150
Ted Kremeneked30e8d2008-04-16 17:07:59 +0000151 if (!E)
152 return;
Mike Stump11289f42009-09-09 15:08:12 +0000153
Ted Kremeneked30e8d2008-04-16 17:07:59 +0000154 assert (getCFG().isBlkExpr(E));
155 LiveState(E, AD) = Alive;
Ted Kremenek8adeebb2008-04-15 04:39:08 +0000156}
157
Ted Kremenekad8bce02007-09-25 04:31:27 +0000158void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
Mike Stump11289f42009-09-09 15:08:12 +0000159 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000160 LiveState(V, AD) = Alive;
161}
162
163void TransferFuncs::VisitBlockExpr(BlockExpr *BE) {
164 AnalysisContext::referenced_decls_iterator I, E;
165 llvm::tie(I, E) = AD.AC->getReferencedBlockVars(BE->getBlockDecl());
166 for ( ; I != E ; ++I) {
167 DeclBitVector_Types::Idx i = AD.getIdx(*I);
168 if (i.isValid())
169 LiveState.getBit(i) = Alive;
170 }
Ted Kremenekb56a9902007-09-06 00:17:54 +0000171}
Mike Stump11289f42009-09-09 15:08:12 +0000172
173void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek1147e362007-09-12 19:10:52 +0000174 if (B->isAssignmentOp()) VisitAssign(B);
175 else VisitStmt(B);
Ted Kremenekb56a9902007-09-06 00:17:54 +0000176}
177
Ted Kremenek65dd30f2008-11-12 21:58:46 +0000178void
179TransferFuncs::BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
Mike Stump11289f42009-09-09 15:08:12 +0000180
Ted Kremenekfc419a02008-11-14 21:07:14 +0000181 // This is a block-level expression. Its value is 'dead' before this point.
182 LiveState(S, AD) = Dead;
183
Ted Kremenek3b4e1d52008-11-11 19:40:47 +0000184 // This represents a 'use' of the collection.
185 Visit(S->getCollection());
Mike Stump11289f42009-09-09 15:08:12 +0000186
Ted Kremenekfbd2f402008-11-11 17:42:10 +0000187 // This represents a 'kill' for the variable.
Ted Kremenek3b4e1d52008-11-11 19:40:47 +0000188 Stmt* Element = S->getElement();
Ted Kremenek99d4ff32008-11-14 01:58:12 +0000189 DeclRefExpr* DR = 0;
Ted Kremenek3b4e1d52008-11-11 19:40:47 +0000190 VarDecl* VD = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000191
Ted Kremenek3b4e1d52008-11-11 19:40:47 +0000192 if (DeclStmt* DS = dyn_cast<DeclStmt>(Element))
Chris Lattner529efc72009-03-28 06:33:19 +0000193 VD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenek3b4e1d52008-11-11 19:40:47 +0000194 else {
Mike Stump11289f42009-09-09 15:08:12 +0000195 Expr* ElemExpr = cast<Expr>(Element)->IgnoreParens();
Ted Kremenek99d4ff32008-11-14 01:58:12 +0000196 if ((DR = dyn_cast<DeclRefExpr>(ElemExpr)))
197 VD = cast<VarDecl>(DR->getDecl());
Ted Kremenekfc419a02008-11-14 21:07:14 +0000198 else {
199 Visit(ElemExpr);
200 return;
201 }
Ted Kremenek3b4e1d52008-11-11 19:40:47 +0000202 }
203
Ted Kremenek99d4ff32008-11-14 01:58:12 +0000204 if (VD) {
205 LiveState(VD, AD) = Dead;
206 if (AD.Observer && DR) { AD.Observer->ObserverKill(DR); }
207 }
Ted Kremenekfbd2f402008-11-11 17:42:10 +0000208}
209
Mike Stump11289f42009-09-09 15:08:12 +0000210
Ted Kremenekad8bce02007-09-25 04:31:27 +0000211void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenekf1dae232008-01-17 17:50:49 +0000212 Expr *E = U->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000213
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000214 switch (U->getOpcode()) {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000215 case UnaryOperator::PostInc:
216 case UnaryOperator::PostDec:
217 case UnaryOperator::PreInc:
218 case UnaryOperator::PreDec:
Ted Kremenekad8bce02007-09-25 04:31:27 +0000219 // Walk through the subexpressions, blasting through ParenExprs
220 // until we either find a DeclRefExpr or some non-DeclRefExpr
221 // expression.
Mike Stump11289f42009-09-09 15:08:12 +0000222 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
Ted Kremenekcd76f952008-04-15 04:08:54 +0000223 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
224 // Treat the --/++ operator as a kill.
Ted Kremenek20c91422008-02-22 00:34:10 +0000225 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
Ted Kremenekcd76f952008-04-15 04:08:54 +0000226 LiveState(VD, AD) = Alive;
Ted Kremenek20c91422008-02-22 00:34:10 +0000227 return VisitDeclRefExpr(DR);
228 }
Ted Kremenek14851c32007-09-28 21:29:33 +0000229
230 // Fall-through.
Mike Stump11289f42009-09-09 15:08:12 +0000231
Ted Kremenekad8bce02007-09-25 04:31:27 +0000232 default:
Ted Kremenekf1dae232008-01-17 17:50:49 +0000233 return Visit(E);
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000234 }
235}
Mike Stump11289f42009-09-09 15:08:12 +0000236
237void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenekf1dae232008-01-17 17:50:49 +0000238 Expr* LHS = B->getLHS();
Ted Kremenekad8bce02007-09-25 04:31:27 +0000239
Ted Kremenek14851c32007-09-28 21:29:33 +0000240 // Assigning to a variable?
Ted Kremenekf1dae232008-01-17 17:50:49 +0000241 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Mike Stump11289f42009-09-09 15:08:12 +0000242
Ted Kremenek82ff6d62008-04-15 18:35:30 +0000243 // Update liveness inforamtion.
244 unsigned bit = AD.getIdx(DR->getDecl());
245 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
Mike Stump11289f42009-09-09 15:08:12 +0000246
Ted Kremenek0064ff42007-09-28 20:38:59 +0000247 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
Mike Stump11289f42009-09-09 15:08:12 +0000248
Ted Kremenekad8bce02007-09-25 04:31:27 +0000249 // Handle things like +=, etc., which also generate "uses"
250 // of a variable. Do this just by visiting the subexpression.
Ted Kremenek14851c32007-09-28 21:29:33 +0000251 if (B->getOpcode() != BinaryOperator::Assign)
252 VisitDeclRefExpr(DR);
Ted Kremenekb56a9902007-09-06 00:17:54 +0000253 }
Ted Kremenekad8bce02007-09-25 04:31:27 +0000254 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000255 Visit(LHS);
Mike Stump11289f42009-09-09 15:08:12 +0000256
Ted Kremenekad8bce02007-09-25 04:31:27 +0000257 Visit(B->getRHS());
Ted Kremenekb56a9902007-09-06 00:17:54 +0000258}
259
Ted Kremenekad8bce02007-09-25 04:31:27 +0000260void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
261 // Declarations effectively "kill" a variable since they cannot
262 // possibly be live before they are declared.
Ted Kremenek4f8792b2008-08-05 20:46:55 +0000263 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE = DS->decl_end();
264 DI != DE; ++DI)
265 if (VarDecl* VD = dyn_cast<VarDecl>(*DI)) {
Ted Kremenek2ece64b2008-09-26 05:52:45 +0000266 // The initializer is evaluated after the variable comes into scope.
267 // Since this is a reverse dataflow analysis, we must evaluate the
268 // transfer function for this expression first.
Ted Kremeneka56c08a2008-02-07 02:38:55 +0000269 if (Expr* Init = VD->getInit())
270 Visit(Init);
Mike Stump11289f42009-09-09 15:08:12 +0000271
Ted Kremenek6ee0a112008-12-09 00:14:14 +0000272 if (const VariableArrayType* VT =
273 AD.getContext().getAsVariableArrayType(VD->getType())) {
274 StmtIterator I(const_cast<VariableArrayType*>(VT));
Mike Stump11289f42009-09-09 15:08:12 +0000275 StmtIterator E;
Ted Kremenek6ee0a112008-12-09 00:14:14 +0000276 for (; I != E; ++I) Visit(*I);
277 }
Mike Stump11289f42009-09-09 15:08:12 +0000278
Ted Kremenek2ece64b2008-09-26 05:52:45 +0000279 // Update liveness information by killing the VarDecl.
280 unsigned bit = AD.getIdx(VD);
281 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
Ted Kremenek7845b262008-02-25 22:28:54 +0000282 }
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000283}
Mike Stump11289f42009-09-09 15:08:12 +0000284
Ted Kremenekb56a9902007-09-06 00:17:54 +0000285} // end anonymous namespace
286
Ted Kremenek0064ff42007-09-28 20:38:59 +0000287//===----------------------------------------------------------------------===//
288// Merge operator: if something is live on any successor block, it is live
289// in the current block (a set union).
Mike Stump11289f42009-09-09 15:08:12 +0000290//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +0000291
Ted Kremenekad8bce02007-09-25 04:31:27 +0000292namespace {
Ted Kremenekb7151c72008-03-20 21:46:49 +0000293
294struct Merge {
Mike Stump11289f42009-09-09 15:08:12 +0000295 typedef StmtDeclBitVector_Types::ValTy ValTy;
296
Ted Kremenekb7151c72008-03-20 21:46:49 +0000297 void operator()(ValTy& Dst, const ValTy& Src) {
298 Dst.OrDeclBits(Src);
Ted Kremenek378e7fd2009-01-30 21:35:30 +0000299 Dst.OrBlkExprBits(Src);
Ted Kremenekb7151c72008-03-20 21:46:49 +0000300 }
301};
Mike Stump11289f42009-09-09 15:08:12 +0000302
Ted Kremenekb7151c72008-03-20 21:46:49 +0000303typedef DataflowSolver<LiveVariables, TransferFuncs, Merge> Solver;
Ted Kremenek0064ff42007-09-28 20:38:59 +0000304} // end anonymous namespace
305
306//===----------------------------------------------------------------------===//
307// External interface to run Liveness analysis.
Mike Stump11289f42009-09-09 15:08:12 +0000308//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +0000309
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +0000310void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000311 Solver S(*this);
312 S.runOnCFG(cfg);
313}
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000314
Ted Kremenekad8bce02007-09-25 04:31:27 +0000315void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenekb4b65e62008-01-17 18:25:22 +0000316 LiveVariables::ObserverTy* Obs,
317 bool recordStmtValues) {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000318 Solver S(*this);
Ted Kremenek975a1192009-11-07 05:57:35 +0000319 SaveAndRestore<LiveVariables::ObserverTy*> SRObs(getAnalysisData().Observer,
320 Obs);
Ted Kremenekb4b65e62008-01-17 18:25:22 +0000321 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000322}
323
324//===----------------------------------------------------------------------===//
325// liveness queries
326//
327
Ted Kremenekbd9cc5c2007-09-10 17:36:42 +0000328bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenek1fdd0a42008-03-13 16:55:07 +0000329 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
330 return i.isValid() ? getBlockData(B).getBit(i) : false;
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000331}
332
Ted Kremenekad8bce02007-09-25 04:31:27 +0000333bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenek1fdd0a42008-03-13 16:55:07 +0000334 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
335 return i.isValid() ? Live.getBit(i) : false;
Ted Kremenek6dc7b112007-09-06 23:00:42 +0000336}
337
Ted Kremenek85be7cf2008-01-17 20:48:37 +0000338bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
339 return getStmtData(Loc)(StmtVal,getAnalysisData());
340}
341
Ted Kremenek05ecfdd2008-01-18 00:40:21 +0000342bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
343 return getStmtData(Loc)(D,getAnalysisData());
344}
345
Ted Kremenek6dc7b112007-09-06 23:00:42 +0000346//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +0000347// printing liveness state for debugging
348//
349
Chris Lattner45540e92009-11-06 18:01:14 +0000350void LiveVariables::dumpLiveness(const ValTy& V, const SourceManager& SM) const {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000351 const AnalysisDataTy& AD = getAnalysisData();
Mike Stump11289f42009-09-09 15:08:12 +0000352
Ted Kremenek0064ff42007-09-28 20:38:59 +0000353 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
354 E = AD.end_decl(); I!=E; ++I)
Mike Stump11289f42009-09-09 15:08:12 +0000355 if (V.getDeclBit(I->second)) {
Daniel Dunbar07d07852009-10-18 21:17:35 +0000356 llvm::errs() << " " << I->first->getIdentifier()->getName() << " <";
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000357 I->first->getLocation().dump(SM);
Daniel Dunbare81a5532009-10-17 18:12:37 +0000358 llvm::errs() << ">\n";
Ted Kremenekb56a9902007-09-06 00:17:54 +0000359 }
Mike Stump11289f42009-09-09 15:08:12 +0000360}
Ted Kremenekb56a9902007-09-06 00:17:54 +0000361
Chris Lattner45540e92009-11-06 18:01:14 +0000362void LiveVariables::dumpBlockLiveness(const SourceManager& M) const {
Jeffrey Yasskin612e3802009-11-10 01:17:45 +0000363 for (BlockDataMapTy::const_iterator I = getBlockDataMap().begin(),
Ted Kremenekad8bce02007-09-25 04:31:27 +0000364 E = getBlockDataMap().end(); I!=E; ++I) {
Daniel Dunbare81a5532009-10-17 18:12:37 +0000365 llvm::errs() << "\n[ B" << I->first->getBlockID()
366 << " (live variables at block exit) ]\n";
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000367 dumpLiveness(I->second,M);
368 }
Ted Kremenekbd9cc5c2007-09-10 17:36:42 +0000369
Daniel Dunbare81a5532009-10-17 18:12:37 +0000370 llvm::errs() << "\n";
Ted Kremenekbd9cc5c2007-09-10 17:36:42 +0000371}