blob: 7896bfcfb0a647236fae0ed3d45f06746a6fef9d [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
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 Kremenek9f9141c2007-11-20 03:01:58 +000089 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek11e72182007-10-01 20:33:52 +000090};
Ted Kremeneke4e63342007-09-06 00:17:54 +000091} // end anonymous namespace
92
Ted Kremenekbffaa832008-01-29 05:13:23 +000093
Ted Kremenek7cb15932008-03-13 16:55:07 +000094LiveVariables::LiveVariables(CFG& cfg) {
95 // Register all referenced VarDecls.
Ted Kremenekbffaa832008-01-29 05:13:23 +000096 getAnalysisData().setCFG(&cfg);
Ted Kremenek11e72182007-10-01 20:33:52 +000097 RegisterDecls R(getAnalysisData());
Ted Kremenekfdd225e2007-09-25 04:31:27 +000098 cfg.VisitBlockStmts(R);
99}
Ted Kremeneke4e63342007-09-06 00:17:54 +0000100
101//===----------------------------------------------------------------------===//
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000102// Transfer functions.
103//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000104
105namespace {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000106
Ted Kremenekc2b51d82008-01-08 18:19:08 +0000107class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000108 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekf63aa452007-09-28 20:38:59 +0000109 LiveVariables::ValTy LiveState;
Ted Kremeneke4e63342007-09-06 00:17:54 +0000110public:
Ted Kremenek11e72182007-10-01 20:33:52 +0000111 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000112
Ted Kremenekf63aa452007-09-28 20:38:59 +0000113 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek9f9141c2007-11-20 03:01:58 +0000114 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenekf1758052007-09-12 19:10:52 +0000115
Ted Kremeneke4e63342007-09-06 00:17:54 +0000116 void VisitDeclRefExpr(DeclRefExpr* DR);
117 void VisitBinaryOperator(BinaryOperator* B);
118 void VisitAssign(BinaryOperator* B);
Ted Kremenek27b07c52007-09-06 21:26:58 +0000119 void VisitDeclStmt(DeclStmt* DS);
120 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremenek37622082008-04-15 04:39:08 +0000121 void Visit(Stmt *S);
122 void VisitTerminator(Stmt* S);
Ted Kremenek7deed0c2008-04-15 18:35:30 +0000123
124 void SetTopValue(LiveVariables::ValTy& V) {
125 V = AD.AlwaysLive;
126 }
127
Ted Kremeneke4e63342007-09-06 00:17:54 +0000128};
Ted Kremenek11e72182007-10-01 20:33:52 +0000129
Ted Kremenek11e72182007-10-01 20:33:52 +0000130void TransferFuncs::Visit(Stmt *S) {
131 if (AD.Observer)
132 AD.Observer->ObserveStmt(S,AD,LiveState);
Ted Kremenekf63aa452007-09-28 20:38:59 +0000133
Ted Kremenek86946742008-01-17 20:48:37 +0000134 if (S == getCurrentBlkStmt()) {
Ted Kremenek1aa9a582008-03-05 19:26:46 +0000135 if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
Ted Kremenek2a9da9c2008-01-18 00:40:21 +0000136 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenek86946742008-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 Kremenek11e72182007-10-01 20:33:52 +0000143}
Ted Kremenek37622082008-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 Kremeneke4e63342007-09-06 00:17:54 +0000161
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000162void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
163 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekf63aa452007-09-28 20:38:59 +0000164 LiveState(V,AD) = Alive;
Ted Kremeneke4e63342007-09-06 00:17:54 +0000165}
Ted Kremeneke4e63342007-09-06 00:17:54 +0000166
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000167void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenekf1758052007-09-12 19:10:52 +0000168 if (B->isAssignmentOp()) VisitAssign(B);
169 else VisitStmt(B);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000170}
171
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000172void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000173 Expr *E = U->getSubExpr();
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000174
Ted Kremenek27b07c52007-09-06 21:26:58 +0000175 switch (U->getOpcode()) {
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000176 case UnaryOperator::SizeOf: return;
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000177 case UnaryOperator::PostInc:
178 case UnaryOperator::PostDec:
179 case UnaryOperator::PreInc:
180 case UnaryOperator::PreDec:
Ted Kremenekfdd225e2007-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 Kremenek56206312008-02-22 00:34:10 +0000184 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
Ted Kremenekdace4c92008-04-15 04:08:54 +0000185 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
186 // Treat the --/++ operator as a kill.
Ted Kremenek56206312008-02-22 00:34:10 +0000187 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
Ted Kremenekdace4c92008-04-15 04:08:54 +0000188 LiveState(VD, AD) = Alive;
Ted Kremenek56206312008-02-22 00:34:10 +0000189 return VisitDeclRefExpr(DR);
190 }
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000191
192 // Fall-through.
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000193
194 default:
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000195 return Visit(E);
Ted Kremenek27b07c52007-09-06 21:26:58 +0000196 }
197}
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000198
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000199void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000200 Expr* LHS = B->getLHS();
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000201
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000202 // Assigning to a variable?
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000203 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenek7deed0c2008-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 Kremenekf63aa452007-09-28 20:38:59 +0000209 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
210
Ted Kremenekfdd225e2007-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 Kremenekbcb07d52007-09-28 21:29:33 +0000213 if (B->getOpcode() != BinaryOperator::Assign)
214 VisitDeclRefExpr(DR);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000215 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000216 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek27b07c52007-09-06 21:26:58 +0000217 Visit(LHS);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000218
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000219 Visit(B->getRHS());
Ted Kremeneke4e63342007-09-06 00:17:54 +0000220}
221
Ted Kremenekfdd225e2007-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 Kremenekdcc48102008-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 Kremenek7deed0c2008-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 Kremenekdcc48102008-02-25 22:28:54 +0000231
Ted Kremenek2bca5e42008-02-07 02:38:55 +0000232 if (Expr* Init = VD->getInit())
233 Visit(Init);
Ted Kremenekdcc48102008-02-25 22:28:54 +0000234 }
Ted Kremenek27b07c52007-09-06 21:26:58 +0000235}
Ted Kremenekf63aa452007-09-28 20:38:59 +0000236
Ted Kremeneke4e63342007-09-06 00:17:54 +0000237} // end anonymous namespace
238
Ted Kremenekf63aa452007-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 Kremeneke4e63342007-09-06 00:17:54 +0000243
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000244namespace {
Ted Kremenekfa59f1f2008-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 Kremenekf63aa452007-09-28 20:38:59 +0000256} // end anonymous namespace
257
258//===----------------------------------------------------------------------===//
259// External interface to run Liveness analysis.
260//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000261
Ted Kremenek83c01da2008-01-11 00:40:29 +0000262void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000263 Solver S(*this);
264 S.runOnCFG(cfg);
265}
Ted Kremenek27b07c52007-09-06 21:26:58 +0000266
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000267void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenek79649df2008-01-17 18:25:22 +0000268 LiveVariables::ObserverTy* Obs,
269 bool recordStmtValues) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000270 Solver S(*this);
271 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenek79649df2008-01-17 18:25:22 +0000272 getAnalysisData().Observer = Obs;
273 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000274 getAnalysisData().Observer = OldObserver;
Ted Kremenek27b07c52007-09-06 21:26:58 +0000275}
276
277//===----------------------------------------------------------------------===//
278// liveness queries
279//
280
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000281bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000282 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
283 return i.isValid() ? getBlockData(B).getBit(i) : false;
Ted Kremenek27b07c52007-09-06 21:26:58 +0000284}
285
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000286bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000287 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
288 return i.isValid() ? Live.getBit(i) : false;
Ted Kremenek055c2752007-09-06 23:00:42 +0000289}
290
Ted Kremenek86946742008-01-17 20:48:37 +0000291bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
292 return getStmtData(Loc)(StmtVal,getAnalysisData());
293}
294
Ted Kremenek2a9da9c2008-01-18 00:40:21 +0000295bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
296 return getStmtData(Loc)(D,getAnalysisData());
297}
298
Ted Kremenek055c2752007-09-06 23:00:42 +0000299//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000300// printing liveness state for debugging
301//
302
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000303void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
304 const AnalysisDataTy& AD = getAnalysisData();
305
Ted Kremenekf63aa452007-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 Kremenek27b07c52007-09-06 21:26:58 +0000309 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000310
Ted Kremenek27b07c52007-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 Kremeneke4e63342007-09-06 00:17:54 +0000316 }
Ted Kremeneke4e63342007-09-06 00:17:54 +0000317}
318
Ted Kremenek27b07c52007-09-06 21:26:58 +0000319void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekfdd225e2007-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 Kremenek27b07c52007-09-06 21:26:58 +0000323 I->first->getBlockID());
324
325 dumpLiveness(I->second,M);
326 }
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000327
328 fprintf(stderr,"\n");
329}