blob: f5a7fb40c77c220ff81fbd92d87f658fdf46a69e [file] [log] [blame]
Ted Kremenek26e47462007-09-20 21:42:55 +00001//=- LiveVariables.cpp - Live Variable Analysis for Source CFGs -*- C++ --*-==//
Ted Kremenekaa04c512007-09-06 00:17:54 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Kremenekd7a2f812007-09-25 04:31:27 +00007// details.
Ted Kremenekaa04c512007-09-06 00:17:54 +00008//
9//===----------------------------------------------------------------------===//
10//
11// This file implements Live Variables analysis for source-level CFGs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekcdf8e842007-12-21 21:42:19 +000015#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek05334682007-09-06 21:26:58 +000016#include "clang/Basic/SourceManager.h"
Ted Kremenekaa04c512007-09-06 00:17:54 +000017#include "clang/AST/Expr.h"
18#include "clang/AST/CFG.h"
Ted Kremenekd7a2f812007-09-25 04:31:27 +000019#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenek10d80462007-09-25 21:00:24 +000020#include "clang/Analysis/FlowSensitive/DataflowSolver.h"
Ted Kremenekaa04c512007-09-06 00:17:54 +000021#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek9ea943f2008-04-15 18:35:30 +000022#include "llvm/ADT/SmallVector.h"
Ted Kremenekec818352008-01-08 18:19:08 +000023#include "llvm/Support/Compiler.h"
Ted Kremenekaa04c512007-09-06 00:17:54 +000024
Ted Kremenek05334682007-09-06 21:26:58 +000025#include <string.h>
26#include <stdio.h>
Ted Kremenekaa04c512007-09-06 00:17:54 +000027
28using namespace clang;
29
30//===----------------------------------------------------------------------===//
Ted Kremenek9ea943f2008-04-15 18:35:30 +000031// Useful constants.
32//===----------------------------------------------------------------------===//
33
34static const bool Alive = true;
35static const bool Dead = false;
36
37//===----------------------------------------------------------------------===//
Ted Kremenekd7a2f812007-09-25 04:31:27 +000038// Dataflow initialization logic.
39//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +000040
41namespace {
Ted Kremenekec818352008-01-08 18:19:08 +000042class VISIBILITY_HIDDEN RegisterDecls
43 : public CFGRecStmtDeclVisitor<RegisterDecls> {
44
Ted Kremenekd7a2f812007-09-25 04:31:27 +000045 LiveVariables::AnalysisDataTy& AD;
Ted Kremenek9ea943f2008-04-15 18:35:30 +000046
47 typedef llvm::SmallVector<VarDecl*, 20> AlwaysLiveTy;
48 AlwaysLiveTy AlwaysLive;
49
50
Ted Kremenekeebf1312007-09-28 20:38:59 +000051public:
Ted Kremenek9ea943f2008-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 Lattner8c7c6a12008-06-17 18:05:57 +000063 void VisitImplicitParamDecl(ImplicitParamDecl* IPD) {
64 // Register the VarDecl for tracking.
65 AD.Register(IPD);
66 }
67
Ted Kremenek9ea943f2008-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 Kremenek705386b2007-11-20 03:01:58 +000094 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek8ce772b2007-10-01 20:33:52 +000095};
Ted Kremenekaa04c512007-09-06 00:17:54 +000096} // end anonymous namespace
97
Ted Kremenekd03aece2008-01-29 05:13:23 +000098
Ted Kremenekf41ac5f2008-03-13 16:55:07 +000099LiveVariables::LiveVariables(CFG& cfg) {
100 // Register all referenced VarDecls.
Ted Kremenekd03aece2008-01-29 05:13:23 +0000101 getAnalysisData().setCFG(&cfg);
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000102 RegisterDecls R(getAnalysisData());
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000103 cfg.VisitBlockStmts(R);
104}
Ted Kremenekaa04c512007-09-06 00:17:54 +0000105
106//===----------------------------------------------------------------------===//
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000107// Transfer functions.
108//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000109
110namespace {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000111
Ted Kremenekec818352008-01-08 18:19:08 +0000112class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000113 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekeebf1312007-09-28 20:38:59 +0000114 LiveVariables::ValTy LiveState;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000115public:
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000116 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000117
Ted Kremenekeebf1312007-09-28 20:38:59 +0000118 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek705386b2007-11-20 03:01:58 +0000119 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek00b63272007-09-12 19:10:52 +0000120
Ted Kremenekaa04c512007-09-06 00:17:54 +0000121 void VisitDeclRefExpr(DeclRefExpr* DR);
122 void VisitBinaryOperator(BinaryOperator* B);
123 void VisitAssign(BinaryOperator* B);
Ted Kremenek05334682007-09-06 21:26:58 +0000124 void VisitDeclStmt(DeclStmt* DS);
125 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremeneka0aa0b12008-04-15 04:39:08 +0000126 void Visit(Stmt *S);
Ted Kremenek79f0a632008-04-16 21:10:48 +0000127 void VisitTerminator(CFGBlock* B);
Ted Kremenek9ea943f2008-04-15 18:35:30 +0000128
129 void SetTopValue(LiveVariables::ValTy& V) {
130 V = AD.AlwaysLive;
131 }
132
Ted Kremenekaa04c512007-09-06 00:17:54 +0000133};
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000134
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000135void TransferFuncs::Visit(Stmt *S) {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000136
Ted Kremenekab6c5902008-01-17 20:48:37 +0000137 if (S == getCurrentBlkStmt()) {
Ted Kremenekb4677db2008-07-03 22:25:27 +0000138
139 if (AD.Observer)
140 AD.Observer->ObserveStmt(S,AD,LiveState);
141
Ted Kremenek925b3a92008-03-05 19:26:46 +0000142 if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
Ted Kremenek1796e132008-01-18 00:40:21 +0000143 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenekab6c5902008-01-17 20:48:37 +0000144 }
Ted Kremenekb4677db2008-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 Kremenekab6c5902008-01-17 20:48:37 +0000150 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenekb4677db2008-07-03 22:25:27 +0000151
152 }
Ted Kremenekab6c5902008-01-17 20:48:37 +0000153 else
154 // For block-level expressions, mark that they are live.
155 LiveState(S,AD) = Alive;
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000156}
Ted Kremeneka0aa0b12008-04-15 04:39:08 +0000157
Ted Kremenek79f0a632008-04-16 21:10:48 +0000158void TransferFuncs::VisitTerminator(CFGBlock* B) {
159
160 const Expr* E = B->getTerminatorCondition();
161
Ted Kremenek5ae4e832008-04-16 17:07:59 +0000162 if (!E)
163 return;
164
Ted Kremenek5ae4e832008-04-16 17:07:59 +0000165 assert (getCFG().isBlkExpr(E));
166 LiveState(E, AD) = Alive;
Ted Kremeneka0aa0b12008-04-15 04:39:08 +0000167}
168
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000169void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
170 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekeebf1312007-09-28 20:38:59 +0000171 LiveState(V,AD) = Alive;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000172}
Ted Kremenekaa04c512007-09-06 00:17:54 +0000173
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000174void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek00b63272007-09-12 19:10:52 +0000175 if (B->isAssignmentOp()) VisitAssign(B);
176 else VisitStmt(B);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000177}
178
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000179void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek133befa2008-01-17 17:50:49 +0000180 Expr *E = U->getSubExpr();
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000181
Ted Kremenek05334682007-09-06 21:26:58 +0000182 switch (U->getOpcode()) {
Ted Kremeneke33d1002007-12-13 04:47:15 +0000183 case UnaryOperator::SizeOf: return;
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000184 case UnaryOperator::PostInc:
185 case UnaryOperator::PostDec:
186 case UnaryOperator::PreInc:
187 case UnaryOperator::PreDec:
Ted Kremenekd7a2f812007-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 Kremenek2c3041c2008-02-22 00:34:10 +0000191 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
Ted Kremenek96715132008-04-15 04:08:54 +0000192 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
193 // Treat the --/++ operator as a kill.
Ted Kremenek2c3041c2008-02-22 00:34:10 +0000194 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
Ted Kremenek96715132008-04-15 04:08:54 +0000195 LiveState(VD, AD) = Alive;
Ted Kremenek2c3041c2008-02-22 00:34:10 +0000196 return VisitDeclRefExpr(DR);
197 }
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000198
199 // Fall-through.
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000200
201 default:
Ted Kremenek133befa2008-01-17 17:50:49 +0000202 return Visit(E);
Ted Kremenek05334682007-09-06 21:26:58 +0000203 }
204}
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000205
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000206void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek133befa2008-01-17 17:50:49 +0000207 Expr* LHS = B->getLHS();
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000208
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000209 // Assigning to a variable?
Ted Kremenek133befa2008-01-17 17:50:49 +0000210 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenek9ea943f2008-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 Kremenekeebf1312007-09-28 20:38:59 +0000216 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
217
Ted Kremenekd7a2f812007-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 Kremenek83a6ba12007-09-28 21:29:33 +0000220 if (B->getOpcode() != BinaryOperator::Assign)
221 VisitDeclRefExpr(DR);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000222 }
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000223 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek05334682007-09-06 21:26:58 +0000224 Visit(LHS);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000225
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000226 Visit(B->getRHS());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000227}
228
Ted Kremenekd7a2f812007-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 Kremenekb59f9cf2008-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 Kremenek9ea943f2008-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 Kremenek43c51bd2008-02-25 22:28:54 +0000239
Ted Kremenek0c5fe982008-02-07 02:38:55 +0000240 if (Expr* Init = VD->getInit())
241 Visit(Init);
Ted Kremenek43c51bd2008-02-25 22:28:54 +0000242 }
Ted Kremenek05334682007-09-06 21:26:58 +0000243}
Ted Kremenekeebf1312007-09-28 20:38:59 +0000244
Ted Kremenekaa04c512007-09-06 00:17:54 +0000245} // end anonymous namespace
246
Ted Kremenekeebf1312007-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 Kremenekaa04c512007-09-06 00:17:54 +0000251
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000252namespace {
Ted Kremenek9b9f2b92008-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 Kremenekeebf1312007-09-28 20:38:59 +0000264} // end anonymous namespace
265
266//===----------------------------------------------------------------------===//
267// External interface to run Liveness analysis.
268//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000269
Ted Kremenek5ee98a72008-01-11 00:40:29 +0000270void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000271 Solver S(*this);
272 S.runOnCFG(cfg);
273}
Ted Kremenek05334682007-09-06 21:26:58 +0000274
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000275void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenekab8ed952008-01-17 18:25:22 +0000276 LiveVariables::ObserverTy* Obs,
277 bool recordStmtValues) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000278 Solver S(*this);
279 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenekab8ed952008-01-17 18:25:22 +0000280 getAnalysisData().Observer = Obs;
281 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000282 getAnalysisData().Observer = OldObserver;
Ted Kremenek05334682007-09-06 21:26:58 +0000283}
284
285//===----------------------------------------------------------------------===//
286// liveness queries
287//
288
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000289bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000290 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
291 return i.isValid() ? getBlockData(B).getBit(i) : false;
Ted Kremenek05334682007-09-06 21:26:58 +0000292}
293
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000294bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000295 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
296 return i.isValid() ? Live.getBit(i) : false;
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000297}
298
Ted Kremenekab6c5902008-01-17 20:48:37 +0000299bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
300 return getStmtData(Loc)(StmtVal,getAnalysisData());
301}
302
Ted Kremenek1796e132008-01-18 00:40:21 +0000303bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
304 return getStmtData(Loc)(D,getAnalysisData());
305}
306
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000307//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000308// printing liveness state for debugging
309//
310
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000311void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
312 const AnalysisDataTy& AD = getAnalysisData();
313
Ted Kremenekeebf1312007-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 Kremenek05334682007-09-06 21:26:58 +0000317 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000318
Ted Kremenek05334682007-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 Kremenekaa04c512007-09-06 00:17:54 +0000324 }
Ted Kremenekaa04c512007-09-06 00:17:54 +0000325}
326
Ted Kremenek05334682007-09-06 21:26:58 +0000327void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekd7a2f812007-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 Kremenek05334682007-09-06 21:26:58 +0000331 I->first->getBlockID());
332
333 dumpLiveness(I->second,M);
334 }
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000335
336 fprintf(stderr,"\n");
337}