blob: f5a7fb40c77c220ff81fbd92d87f658fdf46a69e [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 Kremenekfdd225e2007-09-25 04:31:27 +00007// details.
Ted Kremeneke4e63342007-09-06 00:17:54 +00008//
9//===----------------------------------------------------------------------===//
10//
11// This file implements Live Variables analysis for source-level CFGs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekcf6e41b2007-12-21 21:42:19 +000015#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek27b07c52007-09-06 21:26:58 +000016#include "clang/Basic/SourceManager.h"
Ted Kremeneke4e63342007-09-06 00:17:54 +000017#include "clang/AST/Expr.h"
18#include "clang/AST/CFG.h"
Ted Kremenekfdd225e2007-09-25 04:31:27 +000019#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenek1de632b2007-09-25 21:00:24 +000020#include "clang/Analysis/FlowSensitive/DataflowSolver.h"
Ted Kremeneke4e63342007-09-06 00:17:54 +000021#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek7deed0c2008-04-15 18:35:30 +000022#include "llvm/ADT/SmallVector.h"
Ted Kremenekc2b51d82008-01-08 18:19:08 +000023#include "llvm/Support/Compiler.h"
Ted Kremeneke4e63342007-09-06 00:17:54 +000024
Ted Kremenek27b07c52007-09-06 21:26:58 +000025#include <string.h>
26#include <stdio.h>
Ted Kremeneke4e63342007-09-06 00:17:54 +000027
28using namespace clang;
29
30//===----------------------------------------------------------------------===//
Ted Kremenek7deed0c2008-04-15 18:35:30 +000031// Useful constants.
32//===----------------------------------------------------------------------===//
33
34static const bool Alive = true;
35static const bool Dead = false;
36
37//===----------------------------------------------------------------------===//
Ted Kremenekfdd225e2007-09-25 04:31:27 +000038// Dataflow initialization logic.
39//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +000040
41namespace {
Ted Kremenekc2b51d82008-01-08 18:19:08 +000042class VISIBILITY_HIDDEN RegisterDecls
43 : public CFGRecStmtDeclVisitor<RegisterDecls> {
44
Ted Kremenekfdd225e2007-09-25 04:31:27 +000045 LiveVariables::AnalysisDataTy& AD;
Ted Kremenek7deed0c2008-04-15 18:35:30 +000046
47 typedef llvm::SmallVector<VarDecl*, 20> AlwaysLiveTy;
48 AlwaysLiveTy AlwaysLive;
49
50
Ted Kremenekf63aa452007-09-28 20:38:59 +000051public:
Ted Kremenek7deed0c2008-04-15 18:35:30 +000052 RegisterDecls(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
53
54 ~RegisterDecls() {
55
56 AD.AlwaysLive.resetValues(AD);
57
58 for (AlwaysLiveTy::iterator I = AlwaysLive.begin(), E = AlwaysLive.end();
59 I != E; ++ I)
60 AD.AlwaysLive(*I, AD) = Alive;
61 }
62
Chris Lattner41110242008-06-17 18:05:57 +000063 void VisitImplicitParamDecl(ImplicitParamDecl* IPD) {
64 // Register the VarDecl for tracking.
65 AD.Register(IPD);
66 }
67
Ted Kremenek7deed0c2008-04-15 18:35:30 +000068 void VisitVarDecl(VarDecl* VD) {
69 // Register the VarDecl for tracking.
70 AD.Register(VD);
71
72 // Does the variable have global storage? If so, it is always live.
73 if (VD->hasGlobalStorage())
74 AlwaysLive.push_back(VD);
75 }
76
77 void VisitUnaryOperator(UnaryOperator* U) {
78 // Check for '&'. Any VarDecl whose value has its address-taken we
79 // treat as always being live (flow-insensitive).
80
81 Expr* E = U->getSubExpr()->IgnoreParenCasts();
82
83 if (U->getOpcode() == UnaryOperator::AddrOf)
84 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E))
85 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
86 AD.Register(VD);
87 AlwaysLive.push_back(VD);
88 return;
89 }
90
91 Visit(E);
92 }
93
Ted Kremenek9f9141c2007-11-20 03:01:58 +000094 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek11e72182007-10-01 20:33:52 +000095};
Ted Kremeneke4e63342007-09-06 00:17:54 +000096} // end anonymous namespace
97
Ted Kremenekbffaa832008-01-29 05:13:23 +000098
Ted Kremenek7cb15932008-03-13 16:55:07 +000099LiveVariables::LiveVariables(CFG& cfg) {
100 // Register all referenced VarDecls.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000101 getAnalysisData().setCFG(&cfg);
Ted Kremenek11e72182007-10-01 20:33:52 +0000102 RegisterDecls R(getAnalysisData());
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000103 cfg.VisitBlockStmts(R);
104}
Ted Kremeneke4e63342007-09-06 00:17:54 +0000105
106//===----------------------------------------------------------------------===//
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000107// Transfer functions.
108//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000109
110namespace {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000111
Ted Kremenekc2b51d82008-01-08 18:19:08 +0000112class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000113 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekf63aa452007-09-28 20:38:59 +0000114 LiveVariables::ValTy LiveState;
Ted Kremeneke4e63342007-09-06 00:17:54 +0000115public:
Ted Kremenek11e72182007-10-01 20:33:52 +0000116 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000117
Ted Kremenekf63aa452007-09-28 20:38:59 +0000118 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek9f9141c2007-11-20 03:01:58 +0000119 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenekf1758052007-09-12 19:10:52 +0000120
Ted Kremeneke4e63342007-09-06 00:17:54 +0000121 void VisitDeclRefExpr(DeclRefExpr* DR);
122 void VisitBinaryOperator(BinaryOperator* B);
123 void VisitAssign(BinaryOperator* B);
Ted Kremenek27b07c52007-09-06 21:26:58 +0000124 void VisitDeclStmt(DeclStmt* DS);
125 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremenek37622082008-04-15 04:39:08 +0000126 void Visit(Stmt *S);
Ted Kremenek411cdee2008-04-16 21:10:48 +0000127 void VisitTerminator(CFGBlock* B);
Ted Kremenek7deed0c2008-04-15 18:35:30 +0000128
129 void SetTopValue(LiveVariables::ValTy& V) {
130 V = AD.AlwaysLive;
131 }
132
Ted Kremeneke4e63342007-09-06 00:17:54 +0000133};
Ted Kremenek11e72182007-10-01 20:33:52 +0000134
Ted Kremenek11e72182007-10-01 20:33:52 +0000135void TransferFuncs::Visit(Stmt *S) {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000136
Ted Kremenek86946742008-01-17 20:48:37 +0000137 if (S == getCurrentBlkStmt()) {
Ted Kremenek03648652008-07-03 22:25:27 +0000138
139 if (AD.Observer)
140 AD.Observer->ObserveStmt(S,AD,LiveState);
141
Ted Kremenek1aa9a582008-03-05 19:26:46 +0000142 if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
Ted Kremenek2a9da9c2008-01-18 00:40:21 +0000143 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenek86946742008-01-17 20:48:37 +0000144 }
Ted Kremenek03648652008-07-03 22:25:27 +0000145 else if (!getCFG().isBlkExpr(S)) {
146
147 if (AD.Observer)
148 AD.Observer->ObserveStmt(S,AD,LiveState);
149
Ted Kremenek86946742008-01-17 20:48:37 +0000150 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenek03648652008-07-03 22:25:27 +0000151
152 }
Ted Kremenek86946742008-01-17 20:48:37 +0000153 else
154 // For block-level expressions, mark that they are live.
155 LiveState(S,AD) = Alive;
Ted Kremenek11e72182007-10-01 20:33:52 +0000156}
Ted Kremenek37622082008-04-15 04:39:08 +0000157
Ted Kremenek411cdee2008-04-16 21:10:48 +0000158void TransferFuncs::VisitTerminator(CFGBlock* B) {
159
160 const Expr* E = B->getTerminatorCondition();
161
Ted Kremenek5cd24382008-04-16 17:07:59 +0000162 if (!E)
163 return;
164
Ted Kremenek5cd24382008-04-16 17:07:59 +0000165 assert (getCFG().isBlkExpr(E));
166 LiveState(E, AD) = Alive;
Ted Kremenek37622082008-04-15 04:39:08 +0000167}
168
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000169void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
170 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekf63aa452007-09-28 20:38:59 +0000171 LiveState(V,AD) = Alive;
Ted Kremeneke4e63342007-09-06 00:17:54 +0000172}
Ted Kremeneke4e63342007-09-06 00:17:54 +0000173
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000174void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenekf1758052007-09-12 19:10:52 +0000175 if (B->isAssignmentOp()) VisitAssign(B);
176 else VisitStmt(B);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000177}
178
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000179void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000180 Expr *E = U->getSubExpr();
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000181
Ted Kremenek27b07c52007-09-06 21:26:58 +0000182 switch (U->getOpcode()) {
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000183 case UnaryOperator::SizeOf: return;
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000184 case UnaryOperator::PostInc:
185 case UnaryOperator::PostDec:
186 case UnaryOperator::PreInc:
187 case UnaryOperator::PreDec:
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000188 // Walk through the subexpressions, blasting through ParenExprs
189 // until we either find a DeclRefExpr or some non-DeclRefExpr
190 // expression.
Ted Kremenek56206312008-02-22 00:34:10 +0000191 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
Ted Kremenekdace4c92008-04-15 04:08:54 +0000192 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
193 // Treat the --/++ operator as a kill.
Ted Kremenek56206312008-02-22 00:34:10 +0000194 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
Ted Kremenekdace4c92008-04-15 04:08:54 +0000195 LiveState(VD, AD) = Alive;
Ted Kremenek56206312008-02-22 00:34:10 +0000196 return VisitDeclRefExpr(DR);
197 }
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000198
199 // Fall-through.
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000200
201 default:
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000202 return Visit(E);
Ted Kremenek27b07c52007-09-06 21:26:58 +0000203 }
204}
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000205
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000206void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000207 Expr* LHS = B->getLHS();
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000208
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000209 // Assigning to a variable?
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000210 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenek7deed0c2008-04-15 18:35:30 +0000211
212 // Update liveness inforamtion.
213 unsigned bit = AD.getIdx(DR->getDecl());
214 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
215
Ted Kremenekf63aa452007-09-28 20:38:59 +0000216 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
217
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000218 // Handle things like +=, etc., which also generate "uses"
219 // of a variable. Do this just by visiting the subexpression.
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000220 if (B->getOpcode() != BinaryOperator::Assign)
221 VisitDeclRefExpr(DR);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000222 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000223 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek27b07c52007-09-06 21:26:58 +0000224 Visit(LHS);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000225
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000226 Visit(B->getRHS());
Ted Kremeneke4e63342007-09-06 00:17:54 +0000227}
228
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000229void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
230 // Declarations effectively "kill" a variable since they cannot
231 // possibly be live before they are declared.
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000232 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE = DS->decl_end();
233 DI != DE; ++DI)
234 if (VarDecl* VD = dyn_cast<VarDecl>(*DI)) {
Ted Kremenek7deed0c2008-04-15 18:35:30 +0000235
236 // Update liveness information.
237 unsigned bit = AD.getIdx(VD);
238 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
Ted Kremenekdcc48102008-02-25 22:28:54 +0000239
Ted Kremenek2bca5e42008-02-07 02:38:55 +0000240 if (Expr* Init = VD->getInit())
241 Visit(Init);
Ted Kremenekdcc48102008-02-25 22:28:54 +0000242 }
Ted Kremenek27b07c52007-09-06 21:26:58 +0000243}
Ted Kremenekf63aa452007-09-28 20:38:59 +0000244
Ted Kremeneke4e63342007-09-06 00:17:54 +0000245} // end anonymous namespace
246
Ted Kremenekf63aa452007-09-28 20:38:59 +0000247//===----------------------------------------------------------------------===//
248// Merge operator: if something is live on any successor block, it is live
249// in the current block (a set union).
250//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000251
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000252namespace {
Ted Kremenekfa59f1f2008-03-20 21:46:49 +0000253
254struct Merge {
255 typedef ExprDeclBitVector_Types::ValTy ValTy;
256
257 void operator()(ValTy& Dst, const ValTy& Src) {
258 Dst.OrDeclBits(Src);
259 Dst.AndExprBits(Src);
260 }
261};
262
263typedef DataflowSolver<LiveVariables, TransferFuncs, Merge> Solver;
Ted Kremenekf63aa452007-09-28 20:38:59 +0000264} // end anonymous namespace
265
266//===----------------------------------------------------------------------===//
267// External interface to run Liveness analysis.
268//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000269
Ted Kremenek83c01da2008-01-11 00:40:29 +0000270void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000271 Solver S(*this);
272 S.runOnCFG(cfg);
273}
Ted Kremenek27b07c52007-09-06 21:26:58 +0000274
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000275void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenek79649df2008-01-17 18:25:22 +0000276 LiveVariables::ObserverTy* Obs,
277 bool recordStmtValues) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000278 Solver S(*this);
279 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenek79649df2008-01-17 18:25:22 +0000280 getAnalysisData().Observer = Obs;
281 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000282 getAnalysisData().Observer = OldObserver;
Ted Kremenek27b07c52007-09-06 21:26:58 +0000283}
284
285//===----------------------------------------------------------------------===//
286// liveness queries
287//
288
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000289bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000290 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
291 return i.isValid() ? getBlockData(B).getBit(i) : false;
Ted Kremenek27b07c52007-09-06 21:26:58 +0000292}
293
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000294bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000295 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
296 return i.isValid() ? Live.getBit(i) : false;
Ted Kremenek055c2752007-09-06 23:00:42 +0000297}
298
Ted Kremenek86946742008-01-17 20:48:37 +0000299bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
300 return getStmtData(Loc)(StmtVal,getAnalysisData());
301}
302
Ted Kremenek2a9da9c2008-01-18 00:40:21 +0000303bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
304 return getStmtData(Loc)(D,getAnalysisData());
305}
306
Ted Kremenek055c2752007-09-06 23:00:42 +0000307//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000308// printing liveness state for debugging
309//
310
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000311void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
312 const AnalysisDataTy& AD = getAnalysisData();
313
Ted Kremenekf63aa452007-09-28 20:38:59 +0000314 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
315 E = AD.end_decl(); I!=E; ++I)
316 if (V.getDeclBit(I->second)) {
Ted Kremenek27b07c52007-09-06 21:26:58 +0000317 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000318
Ted Kremenek27b07c52007-09-06 21:26:58 +0000319 fprintf(stderr, " %s <%s:%u:%u>\n",
320 I->first->getIdentifier()->getName(),
321 SM.getSourceName(PhysLoc),
322 SM.getLineNumber(PhysLoc),
323 SM.getColumnNumber(PhysLoc));
Ted Kremeneke4e63342007-09-06 00:17:54 +0000324 }
Ted Kremeneke4e63342007-09-06 00:17:54 +0000325}
326
Ted Kremenek27b07c52007-09-06 21:26:58 +0000327void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000328 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
329 E = getBlockDataMap().end(); I!=E; ++I) {
330 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek27b07c52007-09-06 21:26:58 +0000331 I->first->getBlockID());
332
333 dumpLiveness(I->second,M);
334 }
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000335
336 fprintf(stderr,"\n");
337}