blob: feba80d695560b87b20f1c03fdc53e2858343ca3 [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) {
Ted Kremenek37622082008-04-15 04:39:08 +0000146
Ted Kremenek5cd24382008-04-16 17:07:59 +0000147 Expr* E = NULL;
148
149 switch (S->getStmtClass()) {
150 default:
151 return;
152
153 case Stmt::ForStmtClass:
154 E = cast<ForStmt>(S)->getCond();
155 break;
156
157 case Stmt::WhileStmtClass:
158 E = cast<WhileStmt>(S)->getCond();
159 break;
160
161 case Stmt::DoStmtClass:
162 E = cast<DoStmt>(S)->getCond();
163 break;
164
165 case Stmt::IfStmtClass:
166 E = cast<IfStmt>(S)->getCond();
167 break;
168
169 case Stmt::ChooseExprClass:
170 E = cast<ChooseExpr>(S)->getCond();
171 break;
172
173 case Stmt::IndirectGotoStmtClass:
174 E = cast<IndirectGotoStmt>(S)->getTarget();
175 break;
176
177 case Stmt::SwitchStmtClass:
178 E = cast<SwitchStmt>(S)->getCond();
179 break;
180
181 case Stmt::ConditionalOperatorClass:
182 E = cast<ConditionalOperator>(S)->getCond();
183 break;
184
185 case Stmt::BinaryOperatorClass: // '&&' and '||'
186 E = cast<BinaryOperator>(S)->getLHS();
187 break;
Ted Kremenek37622082008-04-15 04:39:08 +0000188 }
Ted Kremenek5cd24382008-04-16 17:07:59 +0000189
190 if (!E)
191 return;
192
193 E = E->IgnoreParens();
194
195 assert (getCFG().isBlkExpr(E));
196 LiveState(E, AD) = Alive;
Ted Kremenek37622082008-04-15 04:39:08 +0000197}
198
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000199void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
200 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekf63aa452007-09-28 20:38:59 +0000201 LiveState(V,AD) = Alive;
Ted Kremeneke4e63342007-09-06 00:17:54 +0000202}
Ted Kremeneke4e63342007-09-06 00:17:54 +0000203
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000204void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenekf1758052007-09-12 19:10:52 +0000205 if (B->isAssignmentOp()) VisitAssign(B);
206 else VisitStmt(B);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000207}
208
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000209void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000210 Expr *E = U->getSubExpr();
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000211
Ted Kremenek27b07c52007-09-06 21:26:58 +0000212 switch (U->getOpcode()) {
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000213 case UnaryOperator::SizeOf: return;
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000214 case UnaryOperator::PostInc:
215 case UnaryOperator::PostDec:
216 case UnaryOperator::PreInc:
217 case UnaryOperator::PreDec:
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000218 // Walk through the subexpressions, blasting through ParenExprs
219 // until we either find a DeclRefExpr or some non-DeclRefExpr
220 // expression.
Ted Kremenek56206312008-02-22 00:34:10 +0000221 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
Ted Kremenekdace4c92008-04-15 04:08:54 +0000222 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
223 // Treat the --/++ operator as a kill.
Ted Kremenek56206312008-02-22 00:34:10 +0000224 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
Ted Kremenekdace4c92008-04-15 04:08:54 +0000225 LiveState(VD, AD) = Alive;
Ted Kremenek56206312008-02-22 00:34:10 +0000226 return VisitDeclRefExpr(DR);
227 }
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000228
229 // Fall-through.
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000230
231 default:
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000232 return Visit(E);
Ted Kremenek27b07c52007-09-06 21:26:58 +0000233 }
234}
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000235
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000236void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000237 Expr* LHS = B->getLHS();
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000238
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000239 // Assigning to a variable?
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000240 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenek7deed0c2008-04-15 18:35:30 +0000241
242 // Update liveness inforamtion.
243 unsigned bit = AD.getIdx(DR->getDecl());
244 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
245
Ted Kremenekf63aa452007-09-28 20:38:59 +0000246 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
247
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000248 // Handle things like +=, etc., which also generate "uses"
249 // of a variable. Do this just by visiting the subexpression.
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000250 if (B->getOpcode() != BinaryOperator::Assign)
251 VisitDeclRefExpr(DR);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000252 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000253 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek27b07c52007-09-06 21:26:58 +0000254 Visit(LHS);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000255
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000256 Visit(B->getRHS());
Ted Kremeneke4e63342007-09-06 00:17:54 +0000257}
258
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000259void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
260 // Declarations effectively "kill" a variable since they cannot
261 // possibly be live before they are declared.
Ted Kremenekdcc48102008-02-25 22:28:54 +0000262 for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator())
263 if (VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremenek7deed0c2008-04-15 18:35:30 +0000264
265 // Update liveness information.
266 unsigned bit = AD.getIdx(VD);
267 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
Ted Kremenekdcc48102008-02-25 22:28:54 +0000268
Ted Kremenek2bca5e42008-02-07 02:38:55 +0000269 if (Expr* Init = VD->getInit())
270 Visit(Init);
Ted Kremenekdcc48102008-02-25 22:28:54 +0000271 }
Ted Kremenek27b07c52007-09-06 21:26:58 +0000272}
Ted Kremenekf63aa452007-09-28 20:38:59 +0000273
Ted Kremeneke4e63342007-09-06 00:17:54 +0000274} // end anonymous namespace
275
Ted Kremenekf63aa452007-09-28 20:38:59 +0000276//===----------------------------------------------------------------------===//
277// Merge operator: if something is live on any successor block, it is live
278// in the current block (a set union).
279//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000280
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000281namespace {
Ted Kremenekfa59f1f2008-03-20 21:46:49 +0000282
283struct Merge {
284 typedef ExprDeclBitVector_Types::ValTy ValTy;
285
286 void operator()(ValTy& Dst, const ValTy& Src) {
287 Dst.OrDeclBits(Src);
288 Dst.AndExprBits(Src);
289 }
290};
291
292typedef DataflowSolver<LiveVariables, TransferFuncs, Merge> Solver;
Ted Kremenekf63aa452007-09-28 20:38:59 +0000293} // end anonymous namespace
294
295//===----------------------------------------------------------------------===//
296// External interface to run Liveness analysis.
297//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000298
Ted Kremenek83c01da2008-01-11 00:40:29 +0000299void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000300 Solver S(*this);
301 S.runOnCFG(cfg);
302}
Ted Kremenek27b07c52007-09-06 21:26:58 +0000303
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000304void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenek79649df2008-01-17 18:25:22 +0000305 LiveVariables::ObserverTy* Obs,
306 bool recordStmtValues) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000307 Solver S(*this);
308 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenek79649df2008-01-17 18:25:22 +0000309 getAnalysisData().Observer = Obs;
310 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000311 getAnalysisData().Observer = OldObserver;
Ted Kremenek27b07c52007-09-06 21:26:58 +0000312}
313
314//===----------------------------------------------------------------------===//
315// liveness queries
316//
317
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000318bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000319 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
320 return i.isValid() ? getBlockData(B).getBit(i) : false;
Ted Kremenek27b07c52007-09-06 21:26:58 +0000321}
322
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000323bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000324 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
325 return i.isValid() ? Live.getBit(i) : false;
Ted Kremenek055c2752007-09-06 23:00:42 +0000326}
327
Ted Kremenek86946742008-01-17 20:48:37 +0000328bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
329 return getStmtData(Loc)(StmtVal,getAnalysisData());
330}
331
Ted Kremenek2a9da9c2008-01-18 00:40:21 +0000332bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
333 return getStmtData(Loc)(D,getAnalysisData());
334}
335
Ted Kremenek055c2752007-09-06 23:00:42 +0000336//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000337// printing liveness state for debugging
338//
339
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000340void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
341 const AnalysisDataTy& AD = getAnalysisData();
342
Ted Kremenekf63aa452007-09-28 20:38:59 +0000343 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
344 E = AD.end_decl(); I!=E; ++I)
345 if (V.getDeclBit(I->second)) {
Ted Kremenek27b07c52007-09-06 21:26:58 +0000346 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000347
Ted Kremenek27b07c52007-09-06 21:26:58 +0000348 fprintf(stderr, " %s <%s:%u:%u>\n",
349 I->first->getIdentifier()->getName(),
350 SM.getSourceName(PhysLoc),
351 SM.getLineNumber(PhysLoc),
352 SM.getColumnNumber(PhysLoc));
Ted Kremeneke4e63342007-09-06 00:17:54 +0000353 }
Ted Kremeneke4e63342007-09-06 00:17:54 +0000354}
355
Ted Kremenek27b07c52007-09-06 21:26:58 +0000356void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000357 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
358 E = getBlockDataMap().end(); I!=E; ++I) {
359 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek27b07c52007-09-06 21:26:58 +0000360 I->first->getBlockID());
361
362 dumpLiveness(I->second,M);
363 }
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000364
365 fprintf(stderr,"\n");
366}