blob: acc13dead85af8129f93d3cb428b74a66cfb9ed0 [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 Kremenekc2b51d82008-01-08 18:19:08 +000022#include "llvm/Support/Compiler.h"
Ted Kremeneke4e63342007-09-06 00:17:54 +000023
Ted Kremenek27b07c52007-09-06 21:26:58 +000024#include <string.h>
25#include <stdio.h>
Ted Kremeneke4e63342007-09-06 00:17:54 +000026
27using namespace clang;
28
29//===----------------------------------------------------------------------===//
Ted Kremenekfdd225e2007-09-25 04:31:27 +000030// Dataflow initialization logic.
31//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +000032
33namespace {
Ted Kremenekc2b51d82008-01-08 18:19:08 +000034class VISIBILITY_HIDDEN RegisterDecls
35 : public CFGRecStmtDeclVisitor<RegisterDecls> {
36
Ted Kremenekfdd225e2007-09-25 04:31:27 +000037 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekf63aa452007-09-28 20:38:59 +000038public:
Ted Kremenek11e72182007-10-01 20:33:52 +000039 RegisterDecls(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekf63aa452007-09-28 20:38:59 +000040 void VisitVarDecl(VarDecl* VD) { AD.Register(VD); }
Ted Kremenek9f9141c2007-11-20 03:01:58 +000041 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek11e72182007-10-01 20:33:52 +000042};
Ted Kremeneke4e63342007-09-06 00:17:54 +000043} // end anonymous namespace
44
Ted Kremenekbffaa832008-01-29 05:13:23 +000045
Ted Kremenek7cb15932008-03-13 16:55:07 +000046LiveVariables::LiveVariables(CFG& cfg) {
47 // Register all referenced VarDecls.
Ted Kremenekbffaa832008-01-29 05:13:23 +000048 getAnalysisData().setCFG(&cfg);
Ted Kremenek11e72182007-10-01 20:33:52 +000049 RegisterDecls R(getAnalysisData());
Ted Kremenekfdd225e2007-09-25 04:31:27 +000050 cfg.VisitBlockStmts(R);
51}
Ted Kremeneke4e63342007-09-06 00:17:54 +000052
53//===----------------------------------------------------------------------===//
Ted Kremenekfdd225e2007-09-25 04:31:27 +000054// Transfer functions.
55//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +000056
57namespace {
Ted Kremenekf63aa452007-09-28 20:38:59 +000058
59static const bool Alive = true;
60static const bool Dead = false;
61
Ted Kremenekc2b51d82008-01-08 18:19:08 +000062class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekfdd225e2007-09-25 04:31:27 +000063 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekf63aa452007-09-28 20:38:59 +000064 LiveVariables::ValTy LiveState;
Ted Kremeneke4e63342007-09-06 00:17:54 +000065public:
Ted Kremenek11e72182007-10-01 20:33:52 +000066 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekfdd225e2007-09-25 04:31:27 +000067
Ted Kremenekf63aa452007-09-28 20:38:59 +000068 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek9f9141c2007-11-20 03:01:58 +000069 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenekf1758052007-09-12 19:10:52 +000070
Ted Kremeneke4e63342007-09-06 00:17:54 +000071 void VisitDeclRefExpr(DeclRefExpr* DR);
72 void VisitBinaryOperator(BinaryOperator* B);
73 void VisitAssign(BinaryOperator* B);
Ted Kremenek27b07c52007-09-06 21:26:58 +000074 void VisitDeclStmt(DeclStmt* DS);
75 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremenek11e72182007-10-01 20:33:52 +000076 void Visit(Stmt *S);
Ted Kremeneke4e63342007-09-06 00:17:54 +000077};
Ted Kremenek11e72182007-10-01 20:33:52 +000078
Ted Kremenek11e72182007-10-01 20:33:52 +000079void TransferFuncs::Visit(Stmt *S) {
80 if (AD.Observer)
81 AD.Observer->ObserveStmt(S,AD,LiveState);
Ted Kremenekf63aa452007-09-28 20:38:59 +000082
Ted Kremenek86946742008-01-17 20:48:37 +000083 if (S == getCurrentBlkStmt()) {
Ted Kremenek1aa9a582008-03-05 19:26:46 +000084 if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
Ted Kremenek2a9da9c2008-01-18 00:40:21 +000085 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenek86946742008-01-17 20:48:37 +000086 }
87 else if (!getCFG().isBlkExpr(S))
88 StmtVisitor<TransferFuncs,void>::Visit(S);
89 else
90 // For block-level expressions, mark that they are live.
91 LiveState(S,AD) = Alive;
Ted Kremenek11e72182007-10-01 20:33:52 +000092}
Ted Kremeneke4e63342007-09-06 00:17:54 +000093
Ted Kremenekfdd225e2007-09-25 04:31:27 +000094void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
95 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekf63aa452007-09-28 20:38:59 +000096 LiveState(V,AD) = Alive;
Ted Kremeneke4e63342007-09-06 00:17:54 +000097}
Ted Kremeneke4e63342007-09-06 00:17:54 +000098
Ted Kremenekfdd225e2007-09-25 04:31:27 +000099void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenekf1758052007-09-12 19:10:52 +0000100 if (B->isAssignmentOp()) VisitAssign(B);
101 else VisitStmt(B);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000102}
103
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000104void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000105 Expr *E = U->getSubExpr();
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000106
Ted Kremenek27b07c52007-09-06 21:26:58 +0000107 switch (U->getOpcode()) {
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000108 case UnaryOperator::SizeOf: return;
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000109 case UnaryOperator::PostInc:
110 case UnaryOperator::PostDec:
111 case UnaryOperator::PreInc:
112 case UnaryOperator::PreDec:
113 case UnaryOperator::AddrOf:
114 // Walk through the subexpressions, blasting through ParenExprs
115 // until we either find a DeclRefExpr or some non-DeclRefExpr
116 // expression.
Ted Kremenek56206312008-02-22 00:34:10 +0000117 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
118 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
119 // Treat the --/++/& operator as a kill.
120 LiveState(VD, AD) = Dead;
121 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
122 return VisitDeclRefExpr(DR);
123 }
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000124
125 // Fall-through.
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000126
127 default:
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000128 return Visit(E);
Ted Kremenek27b07c52007-09-06 21:26:58 +0000129 }
130}
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000131
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000132void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000133 Expr* LHS = B->getLHS();
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000134
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000135 // Assigning to a variable?
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000136 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000137 LiveState(DR->getDecl(),AD) = Dead;
138 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
139
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000140 // Handle things like +=, etc., which also generate "uses"
141 // of a variable. Do this just by visiting the subexpression.
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000142 if (B->getOpcode() != BinaryOperator::Assign)
143 VisitDeclRefExpr(DR);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000144 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000145 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek27b07c52007-09-06 21:26:58 +0000146 Visit(LHS);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000147
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000148 Visit(B->getRHS());
Ted Kremeneke4e63342007-09-06 00:17:54 +0000149}
150
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000151void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
152 // Declarations effectively "kill" a variable since they cannot
153 // possibly be live before they are declared.
Ted Kremenekdcc48102008-02-25 22:28:54 +0000154 for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator())
155 if (VarDecl* VD = dyn_cast<VarDecl>(D)) {
156 LiveState(D,AD) = Dead;
157
Ted Kremenek2bca5e42008-02-07 02:38:55 +0000158 if (Expr* Init = VD->getInit())
159 Visit(Init);
Ted Kremenekdcc48102008-02-25 22:28:54 +0000160 }
Ted Kremenek27b07c52007-09-06 21:26:58 +0000161}
Ted Kremenekf63aa452007-09-28 20:38:59 +0000162
Ted Kremeneke4e63342007-09-06 00:17:54 +0000163} // end anonymous namespace
164
Ted Kremenekf63aa452007-09-28 20:38:59 +0000165//===----------------------------------------------------------------------===//
166// Merge operator: if something is live on any successor block, it is live
167// in the current block (a set union).
168//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000169
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000170namespace {
Ted Kremenekfa59f1f2008-03-20 21:46:49 +0000171
172struct Merge {
173 typedef ExprDeclBitVector_Types::ValTy ValTy;
174
175 void operator()(ValTy& Dst, const ValTy& Src) {
176 Dst.OrDeclBits(Src);
177 Dst.AndExprBits(Src);
178 }
179};
180
181typedef DataflowSolver<LiveVariables, TransferFuncs, Merge> Solver;
Ted Kremenekf63aa452007-09-28 20:38:59 +0000182} // end anonymous namespace
183
184//===----------------------------------------------------------------------===//
185// External interface to run Liveness analysis.
186//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000187
Ted Kremenek83c01da2008-01-11 00:40:29 +0000188void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000189 Solver S(*this);
190 S.runOnCFG(cfg);
191}
Ted Kremenek27b07c52007-09-06 21:26:58 +0000192
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000193void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenek79649df2008-01-17 18:25:22 +0000194 LiveVariables::ObserverTy* Obs,
195 bool recordStmtValues) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000196 Solver S(*this);
197 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenek79649df2008-01-17 18:25:22 +0000198 getAnalysisData().Observer = Obs;
199 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000200 getAnalysisData().Observer = OldObserver;
Ted Kremenek27b07c52007-09-06 21:26:58 +0000201}
202
203//===----------------------------------------------------------------------===//
204// liveness queries
205//
206
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000207bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000208 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
209 return i.isValid() ? getBlockData(B).getBit(i) : false;
Ted Kremenek27b07c52007-09-06 21:26:58 +0000210}
211
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000212bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000213 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
214 return i.isValid() ? Live.getBit(i) : false;
Ted Kremenek055c2752007-09-06 23:00:42 +0000215}
216
Ted Kremenek86946742008-01-17 20:48:37 +0000217bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
218 return getStmtData(Loc)(StmtVal,getAnalysisData());
219}
220
Ted Kremenek2a9da9c2008-01-18 00:40:21 +0000221bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
222 return getStmtData(Loc)(D,getAnalysisData());
223}
224
Ted Kremenek055c2752007-09-06 23:00:42 +0000225//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000226// printing liveness state for debugging
227//
228
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000229void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
230 const AnalysisDataTy& AD = getAnalysisData();
231
Ted Kremenekf63aa452007-09-28 20:38:59 +0000232 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
233 E = AD.end_decl(); I!=E; ++I)
234 if (V.getDeclBit(I->second)) {
Ted Kremenek27b07c52007-09-06 21:26:58 +0000235 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000236
Ted Kremenek27b07c52007-09-06 21:26:58 +0000237 fprintf(stderr, " %s <%s:%u:%u>\n",
238 I->first->getIdentifier()->getName(),
239 SM.getSourceName(PhysLoc),
240 SM.getLineNumber(PhysLoc),
241 SM.getColumnNumber(PhysLoc));
Ted Kremeneke4e63342007-09-06 00:17:54 +0000242 }
Ted Kremeneke4e63342007-09-06 00:17:54 +0000243}
244
Ted Kremenek27b07c52007-09-06 21:26:58 +0000245void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000246 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
247 E = getBlockDataMap().end(); I!=E; ++I) {
248 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek27b07c52007-09-06 21:26:58 +0000249 I->first->getBlockID());
250
251 dumpLiveness(I->second,M);
252 }
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000253
254 fprintf(stderr,"\n");
255}