blob: e0b3f1992cf108e104ce7d21d8a7fae9af168d8a [file] [log] [blame]
Ted Kremenek11de5cb2007-09-20 21:42:55 +00001//=- LiveVariables.cpp - Live Variable Analysis for Source CFGs -*- C++ --*-==//
Ted Kremeneke4e63342007-09-06 00:17:54 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Kremeneke4e63342007-09-06 00:17:54 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Live Variables analysis for source-level CFGs.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekcf6e41b2007-12-21 21:42:19 +000014#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek27b07c52007-09-06 21:26:58 +000015#include "clang/Basic/SourceManager.h"
Ted Kremeneke4e63342007-09-06 00:17:54 +000016#include "clang/AST/Expr.h"
17#include "clang/AST/CFG.h"
Ted Kremenekfdd225e2007-09-25 04:31:27 +000018#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenek1de632b2007-09-25 21:00:24 +000019#include "clang/Analysis/FlowSensitive/DataflowSolver.h"
Ted Kremeneke4e63342007-09-06 00:17:54 +000020#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek7deed0c2008-04-15 18:35:30 +000021#include "llvm/ADT/SmallVector.h"
Ted Kremenekc2b51d82008-01-08 18:19:08 +000022#include "llvm/Support/Compiler.h"
Ted Kremeneke4e63342007-09-06 00:17:54 +000023
Ted Kremenek27b07c52007-09-06 21:26:58 +000024#include <string.h>
25#include <stdio.h>
Ted Kremeneke4e63342007-09-06 00:17:54 +000026
27using namespace clang;
28
29//===----------------------------------------------------------------------===//
Ted Kremenek7deed0c2008-04-15 18:35:30 +000030// Useful constants.
31//===----------------------------------------------------------------------===//
32
33static const bool Alive = true;
34static const bool Dead = false;
35
36//===----------------------------------------------------------------------===//
Ted Kremenekfdd225e2007-09-25 04:31:27 +000037// Dataflow initialization logic.
38//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +000039
40namespace {
Ted Kremenekc2b51d82008-01-08 18:19:08 +000041class VISIBILITY_HIDDEN RegisterDecls
42 : public CFGRecStmtDeclVisitor<RegisterDecls> {
43
Ted Kremenekfdd225e2007-09-25 04:31:27 +000044 LiveVariables::AnalysisDataTy& AD;
Ted Kremenek7deed0c2008-04-15 18:35:30 +000045
46 typedef llvm::SmallVector<VarDecl*, 20> AlwaysLiveTy;
47 AlwaysLiveTy AlwaysLive;
48
49
Ted Kremenekf63aa452007-09-28 20:38:59 +000050public:
Ted Kremenek7deed0c2008-04-15 18:35:30 +000051 RegisterDecls(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
52
53 ~RegisterDecls() {
54
55 AD.AlwaysLive.resetValues(AD);
56
57 for (AlwaysLiveTy::iterator I = AlwaysLive.begin(), E = AlwaysLive.end();
58 I != E; ++ I)
59 AD.AlwaysLive(*I, AD) = Alive;
60 }
61
Chris Lattner41110242008-06-17 18:05:57 +000062 void VisitImplicitParamDecl(ImplicitParamDecl* IPD) {
63 // Register the VarDecl for tracking.
64 AD.Register(IPD);
65 }
66
Ted Kremenek7deed0c2008-04-15 18:35:30 +000067 void VisitVarDecl(VarDecl* VD) {
68 // Register the VarDecl for tracking.
69 AD.Register(VD);
70
71 // Does the variable have global storage? If so, it is always live.
72 if (VD->hasGlobalStorage())
73 AlwaysLive.push_back(VD);
74 }
75
76 void VisitUnaryOperator(UnaryOperator* U) {
77 // Check for '&'. Any VarDecl whose value has its address-taken we
78 // treat as always being live (flow-insensitive).
79
80 Expr* E = U->getSubExpr()->IgnoreParenCasts();
81
82 if (U->getOpcode() == UnaryOperator::AddrOf)
83 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E))
84 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
85 AD.Register(VD);
86 AlwaysLive.push_back(VD);
87 return;
88 }
89
90 Visit(E);
91 }
92
Ted Kremenek9f9141c2007-11-20 03:01:58 +000093 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek11e72182007-10-01 20:33:52 +000094};
Ted Kremeneke4e63342007-09-06 00:17:54 +000095} // end anonymous namespace
96
Ted Kremenekbffaa832008-01-29 05:13:23 +000097
Ted Kremenek7cb15932008-03-13 16:55:07 +000098LiveVariables::LiveVariables(CFG& cfg) {
99 // Register all referenced VarDecls.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000100 getAnalysisData().setCFG(&cfg);
Ted Kremenek11e72182007-10-01 20:33:52 +0000101 RegisterDecls R(getAnalysisData());
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000102 cfg.VisitBlockStmts(R);
103}
Ted Kremeneke4e63342007-09-06 00:17:54 +0000104
105//===----------------------------------------------------------------------===//
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000106// Transfer functions.
107//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000108
109namespace {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000110
Ted Kremenekc2b51d82008-01-08 18:19:08 +0000111class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000112 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekf63aa452007-09-28 20:38:59 +0000113 LiveVariables::ValTy LiveState;
Ted Kremeneke4e63342007-09-06 00:17:54 +0000114public:
Ted Kremenek11e72182007-10-01 20:33:52 +0000115 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000116
Ted Kremenekf63aa452007-09-28 20:38:59 +0000117 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek9f9141c2007-11-20 03:01:58 +0000118 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenekf1758052007-09-12 19:10:52 +0000119
Ted Kremeneke4e63342007-09-06 00:17:54 +0000120 void VisitDeclRefExpr(DeclRefExpr* DR);
121 void VisitBinaryOperator(BinaryOperator* B);
122 void VisitAssign(BinaryOperator* B);
Ted Kremenek27b07c52007-09-06 21:26:58 +0000123 void VisitDeclStmt(DeclStmt* DS);
124 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremenek37622082008-04-15 04:39:08 +0000125 void Visit(Stmt *S);
Ted Kremenek411cdee2008-04-16 21:10:48 +0000126 void VisitTerminator(CFGBlock* B);
Ted Kremenek7deed0c2008-04-15 18:35:30 +0000127
128 void SetTopValue(LiveVariables::ValTy& V) {
129 V = AD.AlwaysLive;
130 }
131
Ted Kremeneke4e63342007-09-06 00:17:54 +0000132};
Ted Kremenek11e72182007-10-01 20:33:52 +0000133
Ted Kremenek11e72182007-10-01 20:33:52 +0000134void TransferFuncs::Visit(Stmt *S) {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000135
Ted Kremenek86946742008-01-17 20:48:37 +0000136 if (S == getCurrentBlkStmt()) {
Ted Kremenek03648652008-07-03 22:25:27 +0000137
138 if (AD.Observer)
139 AD.Observer->ObserveStmt(S,AD,LiveState);
140
Ted Kremenek1aa9a582008-03-05 19:26:46 +0000141 if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
Ted Kremenek2a9da9c2008-01-18 00:40:21 +0000142 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenek86946742008-01-17 20:48:37 +0000143 }
Ted Kremenek03648652008-07-03 22:25:27 +0000144 else if (!getCFG().isBlkExpr(S)) {
145
146 if (AD.Observer)
147 AD.Observer->ObserveStmt(S,AD,LiveState);
148
Ted Kremenek86946742008-01-17 20:48:37 +0000149 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenek03648652008-07-03 22:25:27 +0000150
151 }
Ted Kremenek86946742008-01-17 20:48:37 +0000152 else
153 // For block-level expressions, mark that they are live.
154 LiveState(S,AD) = Alive;
Ted Kremenek11e72182007-10-01 20:33:52 +0000155}
Ted Kremenek37622082008-04-15 04:39:08 +0000156
Ted Kremenek411cdee2008-04-16 21:10:48 +0000157void TransferFuncs::VisitTerminator(CFGBlock* B) {
158
159 const Expr* E = B->getTerminatorCondition();
160
Ted Kremenek5cd24382008-04-16 17:07:59 +0000161 if (!E)
162 return;
163
Ted Kremenek5cd24382008-04-16 17:07:59 +0000164 assert (getCFG().isBlkExpr(E));
165 LiveState(E, AD) = Alive;
Ted Kremenek37622082008-04-15 04:39:08 +0000166}
167
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000168void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
169 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekf63aa452007-09-28 20:38:59 +0000170 LiveState(V,AD) = Alive;
Ted Kremeneke4e63342007-09-06 00:17:54 +0000171}
Ted Kremeneke4e63342007-09-06 00:17:54 +0000172
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000173void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenekf1758052007-09-12 19:10:52 +0000174 if (B->isAssignmentOp()) VisitAssign(B);
175 else VisitStmt(B);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000176}
177
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000178void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000179 Expr *E = U->getSubExpr();
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000180
Ted Kremenek27b07c52007-09-06 21:26:58 +0000181 switch (U->getOpcode()) {
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000182 case UnaryOperator::SizeOf: return;
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000183 case UnaryOperator::PostInc:
184 case UnaryOperator::PostDec:
185 case UnaryOperator::PreInc:
186 case UnaryOperator::PreDec:
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000187 // Walk through the subexpressions, blasting through ParenExprs
188 // until we either find a DeclRefExpr or some non-DeclRefExpr
189 // expression.
Ted Kremenek56206312008-02-22 00:34:10 +0000190 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
Ted Kremenekdace4c92008-04-15 04:08:54 +0000191 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
192 // Treat the --/++ operator as a kill.
Ted Kremenek56206312008-02-22 00:34:10 +0000193 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
Ted Kremenekdace4c92008-04-15 04:08:54 +0000194 LiveState(VD, AD) = Alive;
Ted Kremenek56206312008-02-22 00:34:10 +0000195 return VisitDeclRefExpr(DR);
196 }
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000197
198 // Fall-through.
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000199
200 default:
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000201 return Visit(E);
Ted Kremenek27b07c52007-09-06 21:26:58 +0000202 }
203}
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000204
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000205void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000206 Expr* LHS = B->getLHS();
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000207
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000208 // Assigning to a variable?
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000209 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenek7deed0c2008-04-15 18:35:30 +0000210
211 // Update liveness inforamtion.
212 unsigned bit = AD.getIdx(DR->getDecl());
213 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
214
Ted Kremenekf63aa452007-09-28 20:38:59 +0000215 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
216
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000217 // Handle things like +=, etc., which also generate "uses"
218 // of a variable. Do this just by visiting the subexpression.
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000219 if (B->getOpcode() != BinaryOperator::Assign)
220 VisitDeclRefExpr(DR);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000221 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000222 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek27b07c52007-09-06 21:26:58 +0000223 Visit(LHS);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000224
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000225 Visit(B->getRHS());
Ted Kremeneke4e63342007-09-06 00:17:54 +0000226}
227
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000228void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
229 // Declarations effectively "kill" a variable since they cannot
230 // possibly be live before they are declared.
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000231 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE = DS->decl_end();
232 DI != DE; ++DI)
233 if (VarDecl* VD = dyn_cast<VarDecl>(*DI)) {
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000234 // The initializer is evaluated after the variable comes into scope.
235 // Since this is a reverse dataflow analysis, we must evaluate the
236 // transfer function for this expression first.
Ted Kremenek2bca5e42008-02-07 02:38:55 +0000237 if (Expr* Init = VD->getInit())
238 Visit(Init);
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000239
240 // Update liveness information by killing the VarDecl.
241 unsigned bit = AD.getIdx(VD);
242 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
Ted Kremenekdcc48102008-02-25 22:28:54 +0000243 }
Ted Kremenek27b07c52007-09-06 21:26:58 +0000244}
Ted Kremenekf63aa452007-09-28 20:38:59 +0000245
Ted Kremeneke4e63342007-09-06 00:17:54 +0000246} // end anonymous namespace
247
Ted Kremenekf63aa452007-09-28 20:38:59 +0000248//===----------------------------------------------------------------------===//
249// Merge operator: if something is live on any successor block, it is live
250// in the current block (a set union).
251//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000252
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000253namespace {
Ted Kremenekfa59f1f2008-03-20 21:46:49 +0000254
255struct Merge {
256 typedef ExprDeclBitVector_Types::ValTy ValTy;
257
258 void operator()(ValTy& Dst, const ValTy& Src) {
259 Dst.OrDeclBits(Src);
260 Dst.AndExprBits(Src);
261 }
262};
263
264typedef DataflowSolver<LiveVariables, TransferFuncs, Merge> Solver;
Ted Kremenekf63aa452007-09-28 20:38:59 +0000265} // end anonymous namespace
266
267//===----------------------------------------------------------------------===//
268// External interface to run Liveness analysis.
269//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000270
Ted Kremenek83c01da2008-01-11 00:40:29 +0000271void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000272 Solver S(*this);
273 S.runOnCFG(cfg);
274}
Ted Kremenek27b07c52007-09-06 21:26:58 +0000275
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000276void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenek79649df2008-01-17 18:25:22 +0000277 LiveVariables::ObserverTy* Obs,
278 bool recordStmtValues) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000279 Solver S(*this);
280 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenek79649df2008-01-17 18:25:22 +0000281 getAnalysisData().Observer = Obs;
282 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000283 getAnalysisData().Observer = OldObserver;
Ted Kremenek27b07c52007-09-06 21:26:58 +0000284}
285
286//===----------------------------------------------------------------------===//
287// liveness queries
288//
289
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000290bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000291 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
292 return i.isValid() ? getBlockData(B).getBit(i) : false;
Ted Kremenek27b07c52007-09-06 21:26:58 +0000293}
294
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000295bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000296 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
297 return i.isValid() ? Live.getBit(i) : false;
Ted Kremenek055c2752007-09-06 23:00:42 +0000298}
299
Ted Kremenek86946742008-01-17 20:48:37 +0000300bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
301 return getStmtData(Loc)(StmtVal,getAnalysisData());
302}
303
Ted Kremenek2a9da9c2008-01-18 00:40:21 +0000304bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
305 return getStmtData(Loc)(D,getAnalysisData());
306}
307
Ted Kremenek055c2752007-09-06 23:00:42 +0000308//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000309// printing liveness state for debugging
310//
311
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000312void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
313 const AnalysisDataTy& AD = getAnalysisData();
314
Ted Kremenekf63aa452007-09-28 20:38:59 +0000315 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
316 E = AD.end_decl(); I!=E; ++I)
317 if (V.getDeclBit(I->second)) {
Ted Kremenek27b07c52007-09-06 21:26:58 +0000318 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000319
Ted Kremenek27b07c52007-09-06 21:26:58 +0000320 fprintf(stderr, " %s <%s:%u:%u>\n",
321 I->first->getIdentifier()->getName(),
322 SM.getSourceName(PhysLoc),
323 SM.getLineNumber(PhysLoc),
324 SM.getColumnNumber(PhysLoc));
Ted Kremeneke4e63342007-09-06 00:17:54 +0000325 }
Ted Kremeneke4e63342007-09-06 00:17:54 +0000326}
327
Ted Kremenek27b07c52007-09-06 21:26:58 +0000328void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000329 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
330 E = getBlockDataMap().end(); I!=E; ++I) {
331 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek27b07c52007-09-06 21:26:58 +0000332 I->first->getBlockID());
333
334 dumpLiveness(I->second,M);
335 }
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000336
337 fprintf(stderr,"\n");
338}