blob: eb1b56f37a891358c33562bc298e7e3c1460b34b [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
46LiveVariables::LiveVariables(CFG& cfg, FunctionDecl& FD) {
47 getAnalysisData().setCFG(&cfg);
48
49 for (FunctionDecl::param_iterator I=FD.param_begin(), E=FD.param_end();
50 I !=E; ++I)
51 getAnalysisData().Register(*I);
52
53 // Now register all the other VarDecls;
Ted Kremenek11e72182007-10-01 20:33:52 +000054 RegisterDecls R(getAnalysisData());
Ted Kremenekfdd225e2007-09-25 04:31:27 +000055 cfg.VisitBlockStmts(R);
56}
Ted Kremeneke4e63342007-09-06 00:17:54 +000057
58//===----------------------------------------------------------------------===//
Ted Kremenekfdd225e2007-09-25 04:31:27 +000059// Transfer functions.
60//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +000061
62namespace {
Ted Kremenekf63aa452007-09-28 20:38:59 +000063
64static const bool Alive = true;
65static const bool Dead = false;
66
Ted Kremenekc2b51d82008-01-08 18:19:08 +000067class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekfdd225e2007-09-25 04:31:27 +000068 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekf63aa452007-09-28 20:38:59 +000069 LiveVariables::ValTy LiveState;
Ted Kremeneke4e63342007-09-06 00:17:54 +000070public:
Ted Kremenek11e72182007-10-01 20:33:52 +000071 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekfdd225e2007-09-25 04:31:27 +000072
Ted Kremenekf63aa452007-09-28 20:38:59 +000073 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek9f9141c2007-11-20 03:01:58 +000074 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenekf1758052007-09-12 19:10:52 +000075
Ted Kremeneke4e63342007-09-06 00:17:54 +000076 void VisitDeclRefExpr(DeclRefExpr* DR);
77 void VisitBinaryOperator(BinaryOperator* B);
78 void VisitAssign(BinaryOperator* B);
Ted Kremenek27b07c52007-09-06 21:26:58 +000079 void VisitDeclStmt(DeclStmt* DS);
80 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremenek11e72182007-10-01 20:33:52 +000081 void Visit(Stmt *S);
Ted Kremeneke4e63342007-09-06 00:17:54 +000082};
Ted Kremenek11e72182007-10-01 20:33:52 +000083
Ted Kremenek11e72182007-10-01 20:33:52 +000084void TransferFuncs::Visit(Stmt *S) {
85 if (AD.Observer)
86 AD.Observer->ObserveStmt(S,AD,LiveState);
Ted Kremenekf63aa452007-09-28 20:38:59 +000087
Ted Kremenek86946742008-01-17 20:48:37 +000088 if (S == getCurrentBlkStmt()) {
Ted Kremenek86946742008-01-17 20:48:37 +000089 if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
Ted Kremenek2a9da9c2008-01-18 00:40:21 +000090 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenek86946742008-01-17 20:48:37 +000091 }
92 else if (!getCFG().isBlkExpr(S))
93 StmtVisitor<TransferFuncs,void>::Visit(S);
94 else
95 // For block-level expressions, mark that they are live.
96 LiveState(S,AD) = Alive;
Ted Kremenek11e72182007-10-01 20:33:52 +000097}
Ted Kremeneke4e63342007-09-06 00:17:54 +000098
Ted Kremenekfdd225e2007-09-25 04:31:27 +000099void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
100 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekf63aa452007-09-28 20:38:59 +0000101 LiveState(V,AD) = Alive;
Ted Kremeneke4e63342007-09-06 00:17:54 +0000102}
Ted Kremeneke4e63342007-09-06 00:17:54 +0000103
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000104void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenekf1758052007-09-12 19:10:52 +0000105 if (B->isAssignmentOp()) VisitAssign(B);
106 else VisitStmt(B);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000107}
108
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000109void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000110 Expr *E = U->getSubExpr();
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000111
Ted Kremenek27b07c52007-09-06 21:26:58 +0000112 switch (U->getOpcode()) {
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000113 case UnaryOperator::SizeOf: return;
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000114 case UnaryOperator::PostInc:
115 case UnaryOperator::PostDec:
116 case UnaryOperator::PreInc:
117 case UnaryOperator::PreDec:
118 case UnaryOperator::AddrOf:
119 // Walk through the subexpressions, blasting through ParenExprs
120 // until we either find a DeclRefExpr or some non-DeclRefExpr
121 // expression.
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000122 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000123 // Treat the --/++/& operator as a kill.
124 LiveState(DR->getDecl(),AD) = Dead;
125 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
126 return VisitDeclRefExpr(DR);
Ted Kremenekf63aa452007-09-28 20:38:59 +0000127 }
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000128
129 // Fall-through.
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000130
131 default:
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000132 return Visit(E);
Ted Kremenek27b07c52007-09-06 21:26:58 +0000133 }
134}
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000135
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000136void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000137 Expr* LHS = B->getLHS();
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000138
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000139 // Assigning to a variable?
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000140 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000141 LiveState(DR->getDecl(),AD) = Dead;
142 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
143
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000144 // Handle things like +=, etc., which also generate "uses"
145 // of a variable. Do this just by visiting the subexpression.
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000146 if (B->getOpcode() != BinaryOperator::Assign)
147 VisitDeclRefExpr(DR);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000148 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000149 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek27b07c52007-09-06 21:26:58 +0000150 Visit(LHS);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000151
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000152 Visit(B->getRHS());
Ted Kremeneke4e63342007-09-06 00:17:54 +0000153}
154
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000155void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
156 // Declarations effectively "kill" a variable since they cannot
157 // possibly be live before they are declared.
Ted Kremenek2bca5e42008-02-07 02:38:55 +0000158 for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator()) {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000159 LiveState(D,AD) = Dead;
Ted Kremenek2bca5e42008-02-07 02:38:55 +0000160
161 if (VarDecl* VD = dyn_cast<VarDecl>(D))
162 if (Expr* Init = VD->getInit())
163 Visit(Init);
164 }
Ted Kremenek27b07c52007-09-06 21:26:58 +0000165}
Ted Kremenekf63aa452007-09-28 20:38:59 +0000166
Ted Kremeneke4e63342007-09-06 00:17:54 +0000167} // end anonymous namespace
168
Ted Kremenekf63aa452007-09-28 20:38:59 +0000169//===----------------------------------------------------------------------===//
170// Merge operator: if something is live on any successor block, it is live
171// in the current block (a set union).
172//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000173
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000174namespace {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000175typedef DeclBitVector_Types::Union Merge;
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000176typedef DataflowSolver<LiveVariables,TransferFuncs,Merge> Solver;
Ted Kremenekf63aa452007-09-28 20:38:59 +0000177} // end anonymous namespace
178
179//===----------------------------------------------------------------------===//
180// External interface to run Liveness analysis.
181//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000182
Ted Kremenek83c01da2008-01-11 00:40:29 +0000183void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000184 Solver S(*this);
185 S.runOnCFG(cfg);
186}
Ted Kremenek27b07c52007-09-06 21:26:58 +0000187
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000188void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenek79649df2008-01-17 18:25:22 +0000189 LiveVariables::ObserverTy* Obs,
190 bool recordStmtValues) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000191 Solver S(*this);
192 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenek79649df2008-01-17 18:25:22 +0000193 getAnalysisData().Observer = Obs;
194 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000195 getAnalysisData().Observer = OldObserver;
Ted Kremenek27b07c52007-09-06 21:26:58 +0000196}
197
198//===----------------------------------------------------------------------===//
199// liveness queries
200//
201
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000202bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000203 return getBlockData(B)(D,getAnalysisData());
Ted Kremenek27b07c52007-09-06 21:26:58 +0000204}
205
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000206bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000207 return Live(D,getAnalysisData());
Ted Kremenek055c2752007-09-06 23:00:42 +0000208}
209
Ted Kremenek86946742008-01-17 20:48:37 +0000210bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
211 return getStmtData(Loc)(StmtVal,getAnalysisData());
212}
213
Ted Kremenek2a9da9c2008-01-18 00:40:21 +0000214bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
215 return getStmtData(Loc)(D,getAnalysisData());
216}
217
Ted Kremenek055c2752007-09-06 23:00:42 +0000218//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000219// printing liveness state for debugging
220//
221
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000222void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
223 const AnalysisDataTy& AD = getAnalysisData();
224
Ted Kremenekf63aa452007-09-28 20:38:59 +0000225 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
226 E = AD.end_decl(); I!=E; ++I)
227 if (V.getDeclBit(I->second)) {
Ted Kremenek27b07c52007-09-06 21:26:58 +0000228 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000229
Ted Kremenek27b07c52007-09-06 21:26:58 +0000230 fprintf(stderr, " %s <%s:%u:%u>\n",
231 I->first->getIdentifier()->getName(),
232 SM.getSourceName(PhysLoc),
233 SM.getLineNumber(PhysLoc),
234 SM.getColumnNumber(PhysLoc));
Ted Kremeneke4e63342007-09-06 00:17:54 +0000235 }
Ted Kremeneke4e63342007-09-06 00:17:54 +0000236}
237
Ted Kremenek27b07c52007-09-06 21:26:58 +0000238void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000239 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
240 E = getBlockDataMap().end(); I!=E; ++I) {
241 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek27b07c52007-09-06 21:26:58 +0000242 I->first->getBlockID());
243
244 dumpLiveness(I->second,M);
245 }
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000246
247 fprintf(stderr,"\n");
248}