blob: 54e2195a777d70f61e6fa08d83db2dfafd8e06e3 [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) {
136 if (AD.Observer)
137 AD.Observer->ObserveStmt(S,AD,LiveState);
Ted Kremenekeebf1312007-09-28 20:38:59 +0000138
Ted Kremenekab6c5902008-01-17 20:48:37 +0000139 if (S == getCurrentBlkStmt()) {
Ted Kremenek925b3a92008-03-05 19:26:46 +0000140 if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
Ted Kremenek1796e132008-01-18 00:40:21 +0000141 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenekab6c5902008-01-17 20:48:37 +0000142 }
143 else if (!getCFG().isBlkExpr(S))
144 StmtVisitor<TransferFuncs,void>::Visit(S);
145 else
146 // For block-level expressions, mark that they are live.
147 LiveState(S,AD) = Alive;
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000148}
Ted Kremeneka0aa0b12008-04-15 04:39:08 +0000149
Ted Kremenek79f0a632008-04-16 21:10:48 +0000150void TransferFuncs::VisitTerminator(CFGBlock* B) {
151
152 const Expr* E = B->getTerminatorCondition();
153
Ted Kremenek5ae4e832008-04-16 17:07:59 +0000154 if (!E)
155 return;
156
Ted Kremenek5ae4e832008-04-16 17:07:59 +0000157 assert (getCFG().isBlkExpr(E));
158 LiveState(E, AD) = Alive;
Ted Kremeneka0aa0b12008-04-15 04:39:08 +0000159}
160
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000161void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
162 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekeebf1312007-09-28 20:38:59 +0000163 LiveState(V,AD) = Alive;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000164}
Ted Kremenekaa04c512007-09-06 00:17:54 +0000165
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000166void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek00b63272007-09-12 19:10:52 +0000167 if (B->isAssignmentOp()) VisitAssign(B);
168 else VisitStmt(B);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000169}
170
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000171void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek133befa2008-01-17 17:50:49 +0000172 Expr *E = U->getSubExpr();
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000173
Ted Kremenek05334682007-09-06 21:26:58 +0000174 switch (U->getOpcode()) {
Ted Kremeneke33d1002007-12-13 04:47:15 +0000175 case UnaryOperator::SizeOf: return;
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000176 case UnaryOperator::PostInc:
177 case UnaryOperator::PostDec:
178 case UnaryOperator::PreInc:
179 case UnaryOperator::PreDec:
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000180 // Walk through the subexpressions, blasting through ParenExprs
181 // until we either find a DeclRefExpr or some non-DeclRefExpr
182 // expression.
Ted Kremenek2c3041c2008-02-22 00:34:10 +0000183 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
Ted Kremenek96715132008-04-15 04:08:54 +0000184 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
185 // Treat the --/++ operator as a kill.
Ted Kremenek2c3041c2008-02-22 00:34:10 +0000186 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
Ted Kremenek96715132008-04-15 04:08:54 +0000187 LiveState(VD, AD) = Alive;
Ted Kremenek2c3041c2008-02-22 00:34:10 +0000188 return VisitDeclRefExpr(DR);
189 }
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000190
191 // Fall-through.
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000192
193 default:
Ted Kremenek133befa2008-01-17 17:50:49 +0000194 return Visit(E);
Ted Kremenek05334682007-09-06 21:26:58 +0000195 }
196}
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000197
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000198void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek133befa2008-01-17 17:50:49 +0000199 Expr* LHS = B->getLHS();
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000200
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000201 // Assigning to a variable?
Ted Kremenek133befa2008-01-17 17:50:49 +0000202 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenek9ea943f2008-04-15 18:35:30 +0000203
204 // Update liveness inforamtion.
205 unsigned bit = AD.getIdx(DR->getDecl());
206 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
207
Ted Kremenekeebf1312007-09-28 20:38:59 +0000208 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
209
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000210 // Handle things like +=, etc., which also generate "uses"
211 // of a variable. Do this just by visiting the subexpression.
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000212 if (B->getOpcode() != BinaryOperator::Assign)
213 VisitDeclRefExpr(DR);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000214 }
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000215 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek05334682007-09-06 21:26:58 +0000216 Visit(LHS);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000217
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000218 Visit(B->getRHS());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000219}
220
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000221void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
222 // Declarations effectively "kill" a variable since they cannot
223 // possibly be live before they are declared.
Ted Kremenek43c51bd2008-02-25 22:28:54 +0000224 for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator())
225 if (VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremenek9ea943f2008-04-15 18:35:30 +0000226
227 // Update liveness information.
228 unsigned bit = AD.getIdx(VD);
229 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
Ted Kremenek43c51bd2008-02-25 22:28:54 +0000230
Ted Kremenek0c5fe982008-02-07 02:38:55 +0000231 if (Expr* Init = VD->getInit())
232 Visit(Init);
Ted Kremenek43c51bd2008-02-25 22:28:54 +0000233 }
Ted Kremenek05334682007-09-06 21:26:58 +0000234}
Ted Kremenekeebf1312007-09-28 20:38:59 +0000235
Ted Kremenekaa04c512007-09-06 00:17:54 +0000236} // end anonymous namespace
237
Ted Kremenekeebf1312007-09-28 20:38:59 +0000238//===----------------------------------------------------------------------===//
239// Merge operator: if something is live on any successor block, it is live
240// in the current block (a set union).
241//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000242
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000243namespace {
Ted Kremenek9b9f2b92008-03-20 21:46:49 +0000244
245struct Merge {
246 typedef ExprDeclBitVector_Types::ValTy ValTy;
247
248 void operator()(ValTy& Dst, const ValTy& Src) {
249 Dst.OrDeclBits(Src);
250 Dst.AndExprBits(Src);
251 }
252};
253
254typedef DataflowSolver<LiveVariables, TransferFuncs, Merge> Solver;
Ted Kremenekeebf1312007-09-28 20:38:59 +0000255} // end anonymous namespace
256
257//===----------------------------------------------------------------------===//
258// External interface to run Liveness analysis.
259//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000260
Ted Kremenek5ee98a72008-01-11 00:40:29 +0000261void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000262 Solver S(*this);
263 S.runOnCFG(cfg);
264}
Ted Kremenek05334682007-09-06 21:26:58 +0000265
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000266void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenekab8ed952008-01-17 18:25:22 +0000267 LiveVariables::ObserverTy* Obs,
268 bool recordStmtValues) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000269 Solver S(*this);
270 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenekab8ed952008-01-17 18:25:22 +0000271 getAnalysisData().Observer = Obs;
272 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000273 getAnalysisData().Observer = OldObserver;
Ted Kremenek05334682007-09-06 21:26:58 +0000274}
275
276//===----------------------------------------------------------------------===//
277// liveness queries
278//
279
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000280bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000281 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
282 return i.isValid() ? getBlockData(B).getBit(i) : false;
Ted Kremenek05334682007-09-06 21:26:58 +0000283}
284
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000285bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000286 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
287 return i.isValid() ? Live.getBit(i) : false;
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000288}
289
Ted Kremenekab6c5902008-01-17 20:48:37 +0000290bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
291 return getStmtData(Loc)(StmtVal,getAnalysisData());
292}
293
Ted Kremenek1796e132008-01-18 00:40:21 +0000294bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
295 return getStmtData(Loc)(D,getAnalysisData());
296}
297
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000298//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000299// printing liveness state for debugging
300//
301
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000302void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
303 const AnalysisDataTy& AD = getAnalysisData();
304
Ted Kremenekeebf1312007-09-28 20:38:59 +0000305 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
306 E = AD.end_decl(); I!=E; ++I)
307 if (V.getDeclBit(I->second)) {
Ted Kremenek05334682007-09-06 21:26:58 +0000308 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000309
Ted Kremenek05334682007-09-06 21:26:58 +0000310 fprintf(stderr, " %s <%s:%u:%u>\n",
311 I->first->getIdentifier()->getName(),
312 SM.getSourceName(PhysLoc),
313 SM.getLineNumber(PhysLoc),
314 SM.getColumnNumber(PhysLoc));
Ted Kremenekaa04c512007-09-06 00:17:54 +0000315 }
Ted Kremenekaa04c512007-09-06 00:17:54 +0000316}
317
Ted Kremenek05334682007-09-06 21:26:58 +0000318void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000319 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
320 E = getBlockDataMap().end(); I!=E; ++I) {
321 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek05334682007-09-06 21:26:58 +0000322 I->first->getBlockID());
323
324 dumpLiveness(I->second,M);
325 }
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000326
327 fprintf(stderr,"\n");
328}