blob: 9bbbf55d6ee9a936e7f68fe18b406d7de6fa8239 [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 Kremenekaa04c512007-09-06 00:17:54 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Live Variables analysis for source-level CFGs.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekcdf8e842007-12-21 21:42:19 +000014#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek05334682007-09-06 21:26:58 +000015#include "clang/Basic/SourceManager.h"
Ted Kremenekaa04c512007-09-06 00:17:54 +000016#include "clang/AST/Expr.h"
17#include "clang/AST/CFG.h"
Ted Kremenekd7a2f812007-09-25 04:31:27 +000018#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenek10d80462007-09-25 21:00:24 +000019#include "clang/Analysis/FlowSensitive/DataflowSolver.h"
Ted Kremenekaa04c512007-09-06 00:17:54 +000020#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek9ea943f2008-04-15 18:35:30 +000021#include "llvm/ADT/SmallVector.h"
Ted Kremenekec818352008-01-08 18:19:08 +000022#include "llvm/Support/Compiler.h"
Ted Kremenekaa04c512007-09-06 00:17:54 +000023
Ted Kremenek05334682007-09-06 21:26:58 +000024#include <string.h>
25#include <stdio.h>
Ted Kremenekaa04c512007-09-06 00:17:54 +000026
27using namespace clang;
28
29//===----------------------------------------------------------------------===//
Ted Kremenek9ea943f2008-04-15 18:35:30 +000030// Useful constants.
31//===----------------------------------------------------------------------===//
32
33static const bool Alive = true;
34static const bool Dead = false;
35
36//===----------------------------------------------------------------------===//
Ted Kremenekd7a2f812007-09-25 04:31:27 +000037// Dataflow initialization logic.
38//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +000039
40namespace {
Ted Kremenekec818352008-01-08 18:19:08 +000041class VISIBILITY_HIDDEN RegisterDecls
42 : public CFGRecStmtDeclVisitor<RegisterDecls> {
43
Ted Kremenekd7a2f812007-09-25 04:31:27 +000044 LiveVariables::AnalysisDataTy& AD;
Ted Kremenek9ea943f2008-04-15 18:35:30 +000045
46 typedef llvm::SmallVector<VarDecl*, 20> AlwaysLiveTy;
47 AlwaysLiveTy AlwaysLive;
48
49
Ted Kremenekeebf1312007-09-28 20:38:59 +000050public:
Ted Kremenek9ea943f2008-04-15 18:35:30 +000051 RegisterDecls(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
52
53 ~RegisterDecls() {
54
55 AD.AlwaysLive.resetValues(AD);
56
57 for (AlwaysLiveTy::iterator I = AlwaysLive.begin(), E = AlwaysLive.end();
58 I != E; ++ I)
59 AD.AlwaysLive(*I, AD) = Alive;
60 }
61
Chris Lattner8c7c6a12008-06-17 18:05:57 +000062 void VisitImplicitParamDecl(ImplicitParamDecl* IPD) {
63 // Register the VarDecl for tracking.
64 AD.Register(IPD);
65 }
66
Ted Kremenek9ea943f2008-04-15 18:35:30 +000067 void VisitVarDecl(VarDecl* VD) {
68 // Register the VarDecl for tracking.
69 AD.Register(VD);
70
71 // Does the variable have global storage? If so, it is always live.
72 if (VD->hasGlobalStorage())
73 AlwaysLive.push_back(VD);
74 }
75
76 void VisitUnaryOperator(UnaryOperator* U) {
77 // Check for '&'. Any VarDecl whose value has its address-taken we
78 // treat as always being live (flow-insensitive).
79
80 Expr* E = U->getSubExpr()->IgnoreParenCasts();
81
82 if (U->getOpcode() == UnaryOperator::AddrOf)
83 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E))
84 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
85 AD.Register(VD);
86 AlwaysLive.push_back(VD);
87 return;
88 }
89
90 Visit(E);
91 }
92
Ted Kremenek705386b2007-11-20 03:01:58 +000093 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek8ce772b2007-10-01 20:33:52 +000094};
Ted Kremenekaa04c512007-09-06 00:17:54 +000095} // end anonymous namespace
96
Ted Kremenekd03aece2008-01-29 05:13:23 +000097
Ted Kremenekf41ac5f2008-03-13 16:55:07 +000098LiveVariables::LiveVariables(CFG& cfg) {
99 // Register all referenced VarDecls.
Ted Kremenekd03aece2008-01-29 05:13:23 +0000100 getAnalysisData().setCFG(&cfg);
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000101 RegisterDecls R(getAnalysisData());
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000102 cfg.VisitBlockStmts(R);
103}
Ted Kremenekaa04c512007-09-06 00:17:54 +0000104
105//===----------------------------------------------------------------------===//
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000106// Transfer functions.
107//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000108
109namespace {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000110
Ted Kremenekec818352008-01-08 18:19:08 +0000111class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000112 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekeebf1312007-09-28 20:38:59 +0000113 LiveVariables::ValTy LiveState;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000114public:
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000115 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000116
Ted Kremenekeebf1312007-09-28 20:38:59 +0000117 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek705386b2007-11-20 03:01:58 +0000118 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek00b63272007-09-12 19:10:52 +0000119
Ted Kremenekaa04c512007-09-06 00:17:54 +0000120 void VisitDeclRefExpr(DeclRefExpr* DR);
121 void VisitBinaryOperator(BinaryOperator* B);
122 void VisitAssign(BinaryOperator* B);
Ted Kremenek05334682007-09-06 21:26:58 +0000123 void VisitDeclStmt(DeclStmt* DS);
Ted Kremeneke48db1c2008-11-11 17:42:10 +0000124 void VisitObjCForCollectionStmt(ObjCForCollectionStmt* S);
Ted Kremenek05334682007-09-06 21:26:58 +0000125 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremeneke48db1c2008-11-11 17:42:10 +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
Ted Kremenek0d2bc352008-11-12 21:12:18 +0000160 const Stmt* E = B->getTerminatorCondition();
Ted Kremenek79f0a632008-04-16 21:10:48 +0000161
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 Kremenek612ba102008-11-11 19:40:47 +0000179void TransferFuncs::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
180 // This represents a 'use' of the collection.
181 Visit(S->getCollection());
Ted Kremeneke48db1c2008-11-11 17:42:10 +0000182
183 // This represents a 'kill' for the variable.
Ted Kremenek612ba102008-11-11 19:40:47 +0000184 Stmt* Element = S->getElement();
185 DeclRefExpr *DR;
186 VarDecl* VD = 0;
187
188 if (DeclStmt* DS = dyn_cast<DeclStmt>(Element))
189 VD = cast<VarDecl>(DS->getSolitaryDecl());
190 else {
191 DR = cast<DeclRefExpr>(Element);
192 VD = cast<VarDecl>(DR->getDecl());
193 }
194
195 LiveState(VD, AD) = Dead;
196 if (AD.Observer && DR) { AD.Observer->ObserverKill(DR); }
Ted Kremeneke48db1c2008-11-11 17:42:10 +0000197}
198
199
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000200void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek133befa2008-01-17 17:50:49 +0000201 Expr *E = U->getSubExpr();
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000202
Ted Kremenek05334682007-09-06 21:26:58 +0000203 switch (U->getOpcode()) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000204 case UnaryOperator::PostInc:
205 case UnaryOperator::PostDec:
206 case UnaryOperator::PreInc:
207 case UnaryOperator::PreDec:
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000208 // Walk through the subexpressions, blasting through ParenExprs
209 // until we either find a DeclRefExpr or some non-DeclRefExpr
210 // expression.
Ted Kremenek2c3041c2008-02-22 00:34:10 +0000211 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
Ted Kremenek96715132008-04-15 04:08:54 +0000212 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
213 // Treat the --/++ operator as a kill.
Ted Kremenek2c3041c2008-02-22 00:34:10 +0000214 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
Ted Kremenek96715132008-04-15 04:08:54 +0000215 LiveState(VD, AD) = Alive;
Ted Kremenek2c3041c2008-02-22 00:34:10 +0000216 return VisitDeclRefExpr(DR);
217 }
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000218
219 // Fall-through.
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000220
221 default:
Ted Kremenek133befa2008-01-17 17:50:49 +0000222 return Visit(E);
Ted Kremenek05334682007-09-06 21:26:58 +0000223 }
224}
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000225
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000226void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek133befa2008-01-17 17:50:49 +0000227 Expr* LHS = B->getLHS();
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000228
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000229 // Assigning to a variable?
Ted Kremenek133befa2008-01-17 17:50:49 +0000230 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenek9ea943f2008-04-15 18:35:30 +0000231
232 // Update liveness inforamtion.
233 unsigned bit = AD.getIdx(DR->getDecl());
234 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
235
Ted Kremenekeebf1312007-09-28 20:38:59 +0000236 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
237
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000238 // Handle things like +=, etc., which also generate "uses"
239 // of a variable. Do this just by visiting the subexpression.
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000240 if (B->getOpcode() != BinaryOperator::Assign)
241 VisitDeclRefExpr(DR);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000242 }
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000243 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek05334682007-09-06 21:26:58 +0000244 Visit(LHS);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000245
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000246 Visit(B->getRHS());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000247}
248
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000249void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
250 // Declarations effectively "kill" a variable since they cannot
251 // possibly be live before they are declared.
Ted Kremenekb59f9cf2008-08-05 20:46:55 +0000252 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE = DS->decl_end();
253 DI != DE; ++DI)
254 if (VarDecl* VD = dyn_cast<VarDecl>(*DI)) {
Ted Kremenek85abe6e2008-09-26 05:52:45 +0000255 // The initializer is evaluated after the variable comes into scope.
256 // Since this is a reverse dataflow analysis, we must evaluate the
257 // transfer function for this expression first.
Ted Kremenek0c5fe982008-02-07 02:38:55 +0000258 if (Expr* Init = VD->getInit())
259 Visit(Init);
Ted Kremenek85abe6e2008-09-26 05:52:45 +0000260
261 // Update liveness information by killing the VarDecl.
262 unsigned bit = AD.getIdx(VD);
263 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
Ted Kremenek43c51bd2008-02-25 22:28:54 +0000264 }
Ted Kremenek05334682007-09-06 21:26:58 +0000265}
Ted Kremenekeebf1312007-09-28 20:38:59 +0000266
Ted Kremenekaa04c512007-09-06 00:17:54 +0000267} // end anonymous namespace
268
Ted Kremenekeebf1312007-09-28 20:38:59 +0000269//===----------------------------------------------------------------------===//
270// Merge operator: if something is live on any successor block, it is live
271// in the current block (a set union).
272//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000273
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000274namespace {
Ted Kremenek9b9f2b92008-03-20 21:46:49 +0000275
276struct Merge {
277 typedef ExprDeclBitVector_Types::ValTy ValTy;
278
279 void operator()(ValTy& Dst, const ValTy& Src) {
280 Dst.OrDeclBits(Src);
281 Dst.AndExprBits(Src);
282 }
283};
284
285typedef DataflowSolver<LiveVariables, TransferFuncs, Merge> Solver;
Ted Kremenekeebf1312007-09-28 20:38:59 +0000286} // end anonymous namespace
287
288//===----------------------------------------------------------------------===//
289// External interface to run Liveness analysis.
290//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000291
Ted Kremenek5ee98a72008-01-11 00:40:29 +0000292void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000293 Solver S(*this);
294 S.runOnCFG(cfg);
295}
Ted Kremenek05334682007-09-06 21:26:58 +0000296
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000297void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenekab8ed952008-01-17 18:25:22 +0000298 LiveVariables::ObserverTy* Obs,
299 bool recordStmtValues) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000300 Solver S(*this);
301 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenekab8ed952008-01-17 18:25:22 +0000302 getAnalysisData().Observer = Obs;
303 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000304 getAnalysisData().Observer = OldObserver;
Ted Kremenek05334682007-09-06 21:26:58 +0000305}
306
307//===----------------------------------------------------------------------===//
308// liveness queries
309//
310
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000311bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000312 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
313 return i.isValid() ? getBlockData(B).getBit(i) : false;
Ted Kremenek05334682007-09-06 21:26:58 +0000314}
315
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000316bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000317 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
318 return i.isValid() ? Live.getBit(i) : false;
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000319}
320
Ted Kremenekab6c5902008-01-17 20:48:37 +0000321bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
322 return getStmtData(Loc)(StmtVal,getAnalysisData());
323}
324
Ted Kremenek1796e132008-01-18 00:40:21 +0000325bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
326 return getStmtData(Loc)(D,getAnalysisData());
327}
328
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000329//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000330// printing liveness state for debugging
331//
332
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000333void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
334 const AnalysisDataTy& AD = getAnalysisData();
335
Ted Kremenekeebf1312007-09-28 20:38:59 +0000336 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
337 E = AD.end_decl(); I!=E; ++I)
338 if (V.getDeclBit(I->second)) {
Ted Kremenek05334682007-09-06 21:26:58 +0000339 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000340
Ted Kremenek05334682007-09-06 21:26:58 +0000341 fprintf(stderr, " %s <%s:%u:%u>\n",
342 I->first->getIdentifier()->getName(),
343 SM.getSourceName(PhysLoc),
344 SM.getLineNumber(PhysLoc),
345 SM.getColumnNumber(PhysLoc));
Ted Kremenekaa04c512007-09-06 00:17:54 +0000346 }
Ted Kremenekaa04c512007-09-06 00:17:54 +0000347}
348
Ted Kremenek05334682007-09-06 21:26:58 +0000349void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000350 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
351 E = getBlockDataMap().end(); I!=E; ++I) {
352 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek05334682007-09-06 21:26:58 +0000353 I->first->getBlockID());
354
355 dumpLiveness(I->second,M);
356 }
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000357
358 fprintf(stderr,"\n");
359}