blob: 7896bfcfb0a647236fae0ed3d45f06746a6fef9d [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
63 void VisitVarDecl(VarDecl* VD) {
64 // Register the VarDecl for tracking.
65 AD.Register(VD);
66
67 // Does the variable have global storage? If so, it is always live.
68 if (VD->hasGlobalStorage())
69 AlwaysLive.push_back(VD);
70 }
71
72 void VisitUnaryOperator(UnaryOperator* U) {
73 // Check for '&'. Any VarDecl whose value has its address-taken we
74 // treat as always being live (flow-insensitive).
75
76 Expr* E = U->getSubExpr()->IgnoreParenCasts();
77
78 if (U->getOpcode() == UnaryOperator::AddrOf)
79 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E))
80 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
81 AD.Register(VD);
82 AlwaysLive.push_back(VD);
83 return;
84 }
85
86 Visit(E);
87 }
88
Ted Kremenek705386b2007-11-20 03:01:58 +000089 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek8ce772b2007-10-01 20:33:52 +000090};
Ted Kremenekaa04c512007-09-06 00:17:54 +000091} // end anonymous namespace
92
Ted Kremenekd03aece2008-01-29 05:13:23 +000093
Ted Kremenekf41ac5f2008-03-13 16:55:07 +000094LiveVariables::LiveVariables(CFG& cfg) {
95 // Register all referenced VarDecls.
Ted Kremenekd03aece2008-01-29 05:13:23 +000096 getAnalysisData().setCFG(&cfg);
Ted Kremenek8ce772b2007-10-01 20:33:52 +000097 RegisterDecls R(getAnalysisData());
Ted Kremenekd7a2f812007-09-25 04:31:27 +000098 cfg.VisitBlockStmts(R);
99}
Ted Kremenekaa04c512007-09-06 00:17:54 +0000100
101//===----------------------------------------------------------------------===//
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000102// Transfer functions.
103//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000104
105namespace {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000106
Ted Kremenekec818352008-01-08 18:19:08 +0000107class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000108 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekeebf1312007-09-28 20:38:59 +0000109 LiveVariables::ValTy LiveState;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000110public:
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000111 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000112
Ted Kremenekeebf1312007-09-28 20:38:59 +0000113 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek705386b2007-11-20 03:01:58 +0000114 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek00b63272007-09-12 19:10:52 +0000115
Ted Kremenekaa04c512007-09-06 00:17:54 +0000116 void VisitDeclRefExpr(DeclRefExpr* DR);
117 void VisitBinaryOperator(BinaryOperator* B);
118 void VisitAssign(BinaryOperator* B);
Ted Kremenek05334682007-09-06 21:26:58 +0000119 void VisitDeclStmt(DeclStmt* DS);
120 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremeneka0aa0b12008-04-15 04:39:08 +0000121 void Visit(Stmt *S);
122 void VisitTerminator(Stmt* S);
Ted Kremenek9ea943f2008-04-15 18:35:30 +0000123
124 void SetTopValue(LiveVariables::ValTy& V) {
125 V = AD.AlwaysLive;
126 }
127
Ted Kremenekaa04c512007-09-06 00:17:54 +0000128};
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000129
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000130void TransferFuncs::Visit(Stmt *S) {
131 if (AD.Observer)
132 AD.Observer->ObserveStmt(S,AD,LiveState);
Ted Kremenekeebf1312007-09-28 20:38:59 +0000133
Ted Kremenekab6c5902008-01-17 20:48:37 +0000134 if (S == getCurrentBlkStmt()) {
Ted Kremenek925b3a92008-03-05 19:26:46 +0000135 if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
Ted Kremenek1796e132008-01-18 00:40:21 +0000136 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenekab6c5902008-01-17 20:48:37 +0000137 }
138 else if (!getCFG().isBlkExpr(S))
139 StmtVisitor<TransferFuncs,void>::Visit(S);
140 else
141 // For block-level expressions, mark that they are live.
142 LiveState(S,AD) = Alive;
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000143}
Ted Kremeneka0aa0b12008-04-15 04:39:08 +0000144
145void TransferFuncs::VisitTerminator(Stmt* S) {
146 return;
147
148 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();
149 I != E; ++I) {
150
151 Stmt* Child = *I;
152 if (!Child) continue;
153
154 if (getCFG().isBlkExpr(Child)) {
155 LiveState(Child, AD) = Alive;
156 return; // Only one "condition" expression.
157 }
158 }
159}
160
Ted Kremenekaa04c512007-09-06 00:17:54 +0000161
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000162void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
163 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekeebf1312007-09-28 20:38:59 +0000164 LiveState(V,AD) = Alive;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000165}
Ted Kremenekaa04c512007-09-06 00:17:54 +0000166
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000167void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek00b63272007-09-12 19:10:52 +0000168 if (B->isAssignmentOp()) VisitAssign(B);
169 else VisitStmt(B);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000170}
171
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000172void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek133befa2008-01-17 17:50:49 +0000173 Expr *E = U->getSubExpr();
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000174
Ted Kremenek05334682007-09-06 21:26:58 +0000175 switch (U->getOpcode()) {
Ted Kremeneke33d1002007-12-13 04:47:15 +0000176 case UnaryOperator::SizeOf: return;
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000177 case UnaryOperator::PostInc:
178 case UnaryOperator::PostDec:
179 case UnaryOperator::PreInc:
180 case UnaryOperator::PreDec:
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000181 // Walk through the subexpressions, blasting through ParenExprs
182 // until we either find a DeclRefExpr or some non-DeclRefExpr
183 // expression.
Ted Kremenek2c3041c2008-02-22 00:34:10 +0000184 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
Ted Kremenek96715132008-04-15 04:08:54 +0000185 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
186 // Treat the --/++ operator as a kill.
Ted Kremenek2c3041c2008-02-22 00:34:10 +0000187 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
Ted Kremenek96715132008-04-15 04:08:54 +0000188 LiveState(VD, AD) = Alive;
Ted Kremenek2c3041c2008-02-22 00:34:10 +0000189 return VisitDeclRefExpr(DR);
190 }
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000191
192 // Fall-through.
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000193
194 default:
Ted Kremenek133befa2008-01-17 17:50:49 +0000195 return Visit(E);
Ted Kremenek05334682007-09-06 21:26:58 +0000196 }
197}
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000198
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000199void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek133befa2008-01-17 17:50:49 +0000200 Expr* LHS = B->getLHS();
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000201
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000202 // Assigning to a variable?
Ted Kremenek133befa2008-01-17 17:50:49 +0000203 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenek9ea943f2008-04-15 18:35:30 +0000204
205 // Update liveness inforamtion.
206 unsigned bit = AD.getIdx(DR->getDecl());
207 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
208
Ted Kremenekeebf1312007-09-28 20:38:59 +0000209 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
210
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000211 // Handle things like +=, etc., which also generate "uses"
212 // of a variable. Do this just by visiting the subexpression.
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000213 if (B->getOpcode() != BinaryOperator::Assign)
214 VisitDeclRefExpr(DR);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000215 }
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000216 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek05334682007-09-06 21:26:58 +0000217 Visit(LHS);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000218
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000219 Visit(B->getRHS());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000220}
221
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000222void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
223 // Declarations effectively "kill" a variable since they cannot
224 // possibly be live before they are declared.
Ted Kremenek43c51bd2008-02-25 22:28:54 +0000225 for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator())
226 if (VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremenek9ea943f2008-04-15 18:35:30 +0000227
228 // Update liveness information.
229 unsigned bit = AD.getIdx(VD);
230 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
Ted Kremenek43c51bd2008-02-25 22:28:54 +0000231
Ted Kremenek0c5fe982008-02-07 02:38:55 +0000232 if (Expr* Init = VD->getInit())
233 Visit(Init);
Ted Kremenek43c51bd2008-02-25 22:28:54 +0000234 }
Ted Kremenek05334682007-09-06 21:26:58 +0000235}
Ted Kremenekeebf1312007-09-28 20:38:59 +0000236
Ted Kremenekaa04c512007-09-06 00:17:54 +0000237} // end anonymous namespace
238
Ted Kremenekeebf1312007-09-28 20:38:59 +0000239//===----------------------------------------------------------------------===//
240// Merge operator: if something is live on any successor block, it is live
241// in the current block (a set union).
242//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000243
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000244namespace {
Ted Kremenek9b9f2b92008-03-20 21:46:49 +0000245
246struct Merge {
247 typedef ExprDeclBitVector_Types::ValTy ValTy;
248
249 void operator()(ValTy& Dst, const ValTy& Src) {
250 Dst.OrDeclBits(Src);
251 Dst.AndExprBits(Src);
252 }
253};
254
255typedef DataflowSolver<LiveVariables, TransferFuncs, Merge> Solver;
Ted Kremenekeebf1312007-09-28 20:38:59 +0000256} // end anonymous namespace
257
258//===----------------------------------------------------------------------===//
259// External interface to run Liveness analysis.
260//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000261
Ted Kremenek5ee98a72008-01-11 00:40:29 +0000262void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000263 Solver S(*this);
264 S.runOnCFG(cfg);
265}
Ted Kremenek05334682007-09-06 21:26:58 +0000266
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000267void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenekab8ed952008-01-17 18:25:22 +0000268 LiveVariables::ObserverTy* Obs,
269 bool recordStmtValues) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000270 Solver S(*this);
271 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenekab8ed952008-01-17 18:25:22 +0000272 getAnalysisData().Observer = Obs;
273 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000274 getAnalysisData().Observer = OldObserver;
Ted Kremenek05334682007-09-06 21:26:58 +0000275}
276
277//===----------------------------------------------------------------------===//
278// liveness queries
279//
280
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000281bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000282 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
283 return i.isValid() ? getBlockData(B).getBit(i) : false;
Ted Kremenek05334682007-09-06 21:26:58 +0000284}
285
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000286bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000287 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
288 return i.isValid() ? Live.getBit(i) : false;
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000289}
290
Ted Kremenekab6c5902008-01-17 20:48:37 +0000291bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
292 return getStmtData(Loc)(StmtVal,getAnalysisData());
293}
294
Ted Kremenek1796e132008-01-18 00:40:21 +0000295bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
296 return getStmtData(Loc)(D,getAnalysisData());
297}
298
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000299//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000300// printing liveness state for debugging
301//
302
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000303void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
304 const AnalysisDataTy& AD = getAnalysisData();
305
Ted Kremenekeebf1312007-09-28 20:38:59 +0000306 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
307 E = AD.end_decl(); I!=E; ++I)
308 if (V.getDeclBit(I->second)) {
Ted Kremenek05334682007-09-06 21:26:58 +0000309 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000310
Ted Kremenek05334682007-09-06 21:26:58 +0000311 fprintf(stderr, " %s <%s:%u:%u>\n",
312 I->first->getIdentifier()->getName(),
313 SM.getSourceName(PhysLoc),
314 SM.getLineNumber(PhysLoc),
315 SM.getColumnNumber(PhysLoc));
Ted Kremenekaa04c512007-09-06 00:17:54 +0000316 }
Ted Kremenekaa04c512007-09-06 00:17:54 +0000317}
318
Ted Kremenek05334682007-09-06 21:26:58 +0000319void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000320 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
321 E = getBlockDataMap().end(); I!=E; ++I) {
322 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek05334682007-09-06 21:26:58 +0000323 I->first->getBlockID());
324
325 dumpLiveness(I->second,M);
326 }
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000327
328 fprintf(stderr,"\n");
329}