blob: 4cef30cab8214d445e105d6b78bd91ff5de9f78d [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 Kremenek37622082008-04-15 04:39:08 +000076 void Visit(Stmt *S);
77 void VisitTerminator(Stmt* S);
Ted Kremeneke4e63342007-09-06 00:17:54 +000078};
Ted Kremenek11e72182007-10-01 20:33:52 +000079
Ted Kremenek11e72182007-10-01 20:33:52 +000080void TransferFuncs::Visit(Stmt *S) {
81 if (AD.Observer)
82 AD.Observer->ObserveStmt(S,AD,LiveState);
Ted Kremenekf63aa452007-09-28 20:38:59 +000083
Ted Kremenek86946742008-01-17 20:48:37 +000084 if (S == getCurrentBlkStmt()) {
Ted Kremenek1aa9a582008-03-05 19:26:46 +000085 if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
Ted Kremenek2a9da9c2008-01-18 00:40:21 +000086 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenek86946742008-01-17 20:48:37 +000087 }
88 else if (!getCFG().isBlkExpr(S))
89 StmtVisitor<TransferFuncs,void>::Visit(S);
90 else
91 // For block-level expressions, mark that they are live.
92 LiveState(S,AD) = Alive;
Ted Kremenek11e72182007-10-01 20:33:52 +000093}
Ted Kremenek37622082008-04-15 04:39:08 +000094
95void TransferFuncs::VisitTerminator(Stmt* S) {
96 return;
97
98 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();
99 I != E; ++I) {
100
101 Stmt* Child = *I;
102 if (!Child) continue;
103
104 if (getCFG().isBlkExpr(Child)) {
105 LiveState(Child, AD) = Alive;
106 return; // Only one "condition" expression.
107 }
108 }
109}
110
Ted Kremeneke4e63342007-09-06 00:17:54 +0000111
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000112void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
113 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekf63aa452007-09-28 20:38:59 +0000114 LiveState(V,AD) = Alive;
Ted Kremeneke4e63342007-09-06 00:17:54 +0000115}
Ted Kremeneke4e63342007-09-06 00:17:54 +0000116
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000117void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenekf1758052007-09-12 19:10:52 +0000118 if (B->isAssignmentOp()) VisitAssign(B);
119 else VisitStmt(B);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000120}
121
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000122void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000123 Expr *E = U->getSubExpr();
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000124
Ted Kremenek27b07c52007-09-06 21:26:58 +0000125 switch (U->getOpcode()) {
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000126 case UnaryOperator::SizeOf: return;
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000127 case UnaryOperator::PostInc:
128 case UnaryOperator::PostDec:
129 case UnaryOperator::PreInc:
130 case UnaryOperator::PreDec:
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000131 // Walk through the subexpressions, blasting through ParenExprs
132 // until we either find a DeclRefExpr or some non-DeclRefExpr
133 // expression.
Ted Kremenek56206312008-02-22 00:34:10 +0000134 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
Ted Kremenekdace4c92008-04-15 04:08:54 +0000135 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
136 // Treat the --/++ operator as a kill.
Ted Kremenek56206312008-02-22 00:34:10 +0000137 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
Ted Kremenekdace4c92008-04-15 04:08:54 +0000138 LiveState(VD, AD) = Alive;
Ted Kremenek56206312008-02-22 00:34:10 +0000139 return VisitDeclRefExpr(DR);
140 }
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000141
142 // Fall-through.
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000143
144 default:
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000145 return Visit(E);
Ted Kremenek27b07c52007-09-06 21:26:58 +0000146 }
147}
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000148
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000149void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000150 Expr* LHS = B->getLHS();
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000151
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000152 // Assigning to a variable?
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000153 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenekc8e00b22008-04-15 03:47:30 +0000154 LiveState(DR->getDecl(), AD) = Dead;
Ted Kremenekf63aa452007-09-28 20:38:59 +0000155 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
156
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000157 // Handle things like +=, etc., which also generate "uses"
158 // of a variable. Do this just by visiting the subexpression.
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000159 if (B->getOpcode() != BinaryOperator::Assign)
160 VisitDeclRefExpr(DR);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000161 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000162 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek27b07c52007-09-06 21:26:58 +0000163 Visit(LHS);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000164
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000165 Visit(B->getRHS());
Ted Kremeneke4e63342007-09-06 00:17:54 +0000166}
167
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000168void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
169 // Declarations effectively "kill" a variable since they cannot
170 // possibly be live before they are declared.
Ted Kremenekdcc48102008-02-25 22:28:54 +0000171 for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator())
172 if (VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremenekc8e00b22008-04-15 03:47:30 +0000173 LiveState(D, AD) = Dead;
Ted Kremenekdcc48102008-02-25 22:28:54 +0000174
Ted Kremenek2bca5e42008-02-07 02:38:55 +0000175 if (Expr* Init = VD->getInit())
176 Visit(Init);
Ted Kremenekdcc48102008-02-25 22:28:54 +0000177 }
Ted Kremenek27b07c52007-09-06 21:26:58 +0000178}
Ted Kremenekf63aa452007-09-28 20:38:59 +0000179
Ted Kremeneke4e63342007-09-06 00:17:54 +0000180} // end anonymous namespace
181
Ted Kremenekf63aa452007-09-28 20:38:59 +0000182//===----------------------------------------------------------------------===//
183// Merge operator: if something is live on any successor block, it is live
184// in the current block (a set union).
185//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000186
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000187namespace {
Ted Kremenekfa59f1f2008-03-20 21:46:49 +0000188
189struct Merge {
190 typedef ExprDeclBitVector_Types::ValTy ValTy;
191
192 void operator()(ValTy& Dst, const ValTy& Src) {
193 Dst.OrDeclBits(Src);
194 Dst.AndExprBits(Src);
195 }
196};
197
198typedef DataflowSolver<LiveVariables, TransferFuncs, Merge> Solver;
Ted Kremenekf63aa452007-09-28 20:38:59 +0000199} // end anonymous namespace
200
201//===----------------------------------------------------------------------===//
202// External interface to run Liveness analysis.
203//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000204
Ted Kremenek83c01da2008-01-11 00:40:29 +0000205void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000206 Solver S(*this);
207 S.runOnCFG(cfg);
208}
Ted Kremenek27b07c52007-09-06 21:26:58 +0000209
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000210void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenek79649df2008-01-17 18:25:22 +0000211 LiveVariables::ObserverTy* Obs,
212 bool recordStmtValues) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000213 Solver S(*this);
214 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenek79649df2008-01-17 18:25:22 +0000215 getAnalysisData().Observer = Obs;
216 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000217 getAnalysisData().Observer = OldObserver;
Ted Kremenek27b07c52007-09-06 21:26:58 +0000218}
219
220//===----------------------------------------------------------------------===//
221// liveness queries
222//
223
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000224bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000225 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
226 return i.isValid() ? getBlockData(B).getBit(i) : false;
Ted Kremenek27b07c52007-09-06 21:26:58 +0000227}
228
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000229bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenek7cb15932008-03-13 16:55:07 +0000230 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
231 return i.isValid() ? Live.getBit(i) : false;
Ted Kremenek055c2752007-09-06 23:00:42 +0000232}
233
Ted Kremenek86946742008-01-17 20:48:37 +0000234bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
235 return getStmtData(Loc)(StmtVal,getAnalysisData());
236}
237
Ted Kremenek2a9da9c2008-01-18 00:40:21 +0000238bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
239 return getStmtData(Loc)(D,getAnalysisData());
240}
241
Ted Kremenek055c2752007-09-06 23:00:42 +0000242//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000243// printing liveness state for debugging
244//
245
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000246void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
247 const AnalysisDataTy& AD = getAnalysisData();
248
Ted Kremenekf63aa452007-09-28 20:38:59 +0000249 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
250 E = AD.end_decl(); I!=E; ++I)
251 if (V.getDeclBit(I->second)) {
Ted Kremenek27b07c52007-09-06 21:26:58 +0000252 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000253
Ted Kremenek27b07c52007-09-06 21:26:58 +0000254 fprintf(stderr, " %s <%s:%u:%u>\n",
255 I->first->getIdentifier()->getName(),
256 SM.getSourceName(PhysLoc),
257 SM.getLineNumber(PhysLoc),
258 SM.getColumnNumber(PhysLoc));
Ted Kremeneke4e63342007-09-06 00:17:54 +0000259 }
Ted Kremeneke4e63342007-09-06 00:17:54 +0000260}
261
Ted Kremenek27b07c52007-09-06 21:26:58 +0000262void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000263 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
264 E = getBlockDataMap().end(); I!=E; ++I) {
265 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek27b07c52007-09-06 21:26:58 +0000266 I->first->getBlockID());
267
268 dumpLiveness(I->second,M);
269 }
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000270
271 fprintf(stderr,"\n");
272}