blob: a9322f2d90524de7f9354a0914c83f8c69562b22 [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//
5// This file was developed by Ted Kremenek and is distributed under
Ted Kremenekfdd225e2007-09-25 04:31:27 +00006// the University of Illinois Open Source License. See LICENSE.TXT for
7// 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
15#include "clang/Analysis/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"
22
Ted Kremenek27b07c52007-09-06 21:26:58 +000023#include <string.h>
24#include <stdio.h>
Ted Kremeneke4e63342007-09-06 00:17:54 +000025
26using namespace clang;
27
28//===----------------------------------------------------------------------===//
Ted Kremenekfdd225e2007-09-25 04:31:27 +000029// Dataflow initialization logic.
30//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +000031
32namespace {
Ted Kremenek11e72182007-10-01 20:33:52 +000033class RegisterDecls : public CFGRecStmtDeclVisitor<RegisterDecls> {
Ted Kremenekfdd225e2007-09-25 04:31:27 +000034 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekf63aa452007-09-28 20:38:59 +000035public:
Ted Kremenek11e72182007-10-01 20:33:52 +000036 RegisterDecls(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekf63aa452007-09-28 20:38:59 +000037 void VisitVarDecl(VarDecl* VD) { AD.Register(VD); }
Ted Kremenek9f9141c2007-11-20 03:01:58 +000038 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek11e72182007-10-01 20:33:52 +000039};
Ted Kremeneke4e63342007-09-06 00:17:54 +000040} // end anonymous namespace
41
Ted Kremenekfdd225e2007-09-25 04:31:27 +000042void LiveVariables::InitializeValues(const CFG& cfg) {
Ted Kremenek11e72182007-10-01 20:33:52 +000043 RegisterDecls R(getAnalysisData());
Ted Kremenekfdd225e2007-09-25 04:31:27 +000044 cfg.VisitBlockStmts(R);
45}
Ted Kremeneke4e63342007-09-06 00:17:54 +000046
47//===----------------------------------------------------------------------===//
Ted Kremenekfdd225e2007-09-25 04:31:27 +000048// Transfer functions.
49//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +000050
51namespace {
Ted Kremenekf63aa452007-09-28 20:38:59 +000052
53static const bool Alive = true;
54static const bool Dead = false;
55
Ted Kremenek9f9141c2007-11-20 03:01:58 +000056class TransferFuncs : public CFGRecStmtVisitor<TransferFuncs> {
Ted Kremenekfdd225e2007-09-25 04:31:27 +000057 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekf63aa452007-09-28 20:38:59 +000058 LiveVariables::ValTy LiveState;
Ted Kremeneke4e63342007-09-06 00:17:54 +000059public:
Ted Kremenek11e72182007-10-01 20:33:52 +000060 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekfdd225e2007-09-25 04:31:27 +000061
Ted Kremenekf63aa452007-09-28 20:38:59 +000062 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek9f9141c2007-11-20 03:01:58 +000063 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenekf1758052007-09-12 19:10:52 +000064
Ted Kremeneke4e63342007-09-06 00:17:54 +000065 void VisitDeclRefExpr(DeclRefExpr* DR);
66 void VisitBinaryOperator(BinaryOperator* B);
67 void VisitAssign(BinaryOperator* B);
Ted Kremenek27b07c52007-09-06 21:26:58 +000068 void VisitDeclStmt(DeclStmt* DS);
69 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremenek11e72182007-10-01 20:33:52 +000070 void Visit(Stmt *S);
Ted Kremenekf63aa452007-09-28 20:38:59 +000071
Ted Kremenekbcb07d52007-09-28 21:29:33 +000072 DeclRefExpr* FindDeclRef(Stmt *S);
Ted Kremeneke4e63342007-09-06 00:17:54 +000073};
Ted Kremenek11e72182007-10-01 20:33:52 +000074
Ted Kremenek11e72182007-10-01 20:33:52 +000075void TransferFuncs::Visit(Stmt *S) {
76 if (AD.Observer)
77 AD.Observer->ObserveStmt(S,AD,LiveState);
Ted Kremenekf63aa452007-09-28 20:38:59 +000078
Ted Kremenek11e72182007-10-01 20:33:52 +000079 static_cast<CFGStmtVisitor<TransferFuncs>*>(this)->Visit(S);
80}
Ted Kremeneke4e63342007-09-06 00:17:54 +000081
Ted Kremenekfdd225e2007-09-25 04:31:27 +000082void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
83 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekf63aa452007-09-28 20:38:59 +000084 LiveState(V,AD) = Alive;
Ted Kremeneke4e63342007-09-06 00:17:54 +000085}
Ted Kremeneke4e63342007-09-06 00:17:54 +000086
Ted Kremenekfdd225e2007-09-25 04:31:27 +000087void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenekf1758052007-09-12 19:10:52 +000088 if (B->isAssignmentOp()) VisitAssign(B);
89 else VisitStmt(B);
Ted Kremeneke4e63342007-09-06 00:17:54 +000090}
91
Ted Kremenekfdd225e2007-09-25 04:31:27 +000092void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenekbcb07d52007-09-28 21:29:33 +000093 Stmt *S = U->getSubExpr();
94
Ted Kremenek27b07c52007-09-06 21:26:58 +000095 switch (U->getOpcode()) {
Ted Kremenek8d9ebae2007-12-13 04:47:15 +000096 case UnaryOperator::SizeOf: return;
Ted Kremenekfdd225e2007-09-25 04:31:27 +000097 case UnaryOperator::PostInc:
98 case UnaryOperator::PostDec:
99 case UnaryOperator::PreInc:
100 case UnaryOperator::PreDec:
101 case UnaryOperator::AddrOf:
102 // Walk through the subexpressions, blasting through ParenExprs
103 // until we either find a DeclRefExpr or some non-DeclRefExpr
104 // expression.
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000105 if (DeclRefExpr* DR = FindDeclRef(S)) {
106 // Treat the --/++/& operator as a kill.
107 LiveState(DR->getDecl(),AD) = Dead;
108 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
109 return VisitDeclRefExpr(DR);
Ted Kremenekf63aa452007-09-28 20:38:59 +0000110 }
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000111
112 // Fall-through.
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000113
114 default:
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000115 return Visit(S);
Ted Kremenek27b07c52007-09-06 21:26:58 +0000116 }
117}
118
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000119DeclRefExpr* TransferFuncs::FindDeclRef(Stmt *S) {
120 for (;;)
121 if (ParenExpr* P = dyn_cast<ParenExpr>(S)) {
122 S = P->getSubExpr(); continue;
123 }
124 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
125 return DR;
126 else
127 return NULL;
128}
129
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000130void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremeneke4e63342007-09-06 00:17:54 +0000131 Stmt* LHS = B->getLHS();
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000132
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000133 // Assigning to a variable?
134 if (DeclRefExpr* DR = FindDeclRef(LHS)) {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000135 LiveState(DR->getDecl(),AD) = Dead;
136 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
137
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000138 // Handle things like +=, etc., which also generate "uses"
139 // of a variable. Do this just by visiting the subexpression.
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000140 if (B->getOpcode() != BinaryOperator::Assign)
141 VisitDeclRefExpr(DR);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000142 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000143 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek27b07c52007-09-06 21:26:58 +0000144 Visit(LHS);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000145
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000146 Visit(B->getRHS());
Ted Kremeneke4e63342007-09-06 00:17:54 +0000147}
148
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000149void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
150 // Declarations effectively "kill" a variable since they cannot
151 // possibly be live before they are declared.
Ted Kremenek11e72182007-10-01 20:33:52 +0000152 for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator())
Ted Kremenekf63aa452007-09-28 20:38:59 +0000153 LiveState(D,AD) = Dead;
Ted Kremenek27b07c52007-09-06 21:26:58 +0000154}
Ted Kremenekf63aa452007-09-28 20:38:59 +0000155
Ted Kremeneke4e63342007-09-06 00:17:54 +0000156} // end anonymous namespace
157
Ted Kremenekf63aa452007-09-28 20:38:59 +0000158//===----------------------------------------------------------------------===//
159// Merge operator: if something is live on any successor block, it is live
160// in the current block (a set union).
161//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000162
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000163namespace {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000164typedef DeclBitVector_Types::Union Merge;
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000165typedef DataflowSolver<LiveVariables,TransferFuncs,Merge> Solver;
Ted Kremenekf63aa452007-09-28 20:38:59 +0000166} // end anonymous namespace
167
168//===----------------------------------------------------------------------===//
169// External interface to run Liveness analysis.
170//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000171
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000172void LiveVariables::runOnCFG(const CFG& cfg) {
173 Solver S(*this);
174 S.runOnCFG(cfg);
175}
Ted Kremenek27b07c52007-09-06 21:26:58 +0000176
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000177void LiveVariables::runOnAllBlocks(const CFG& cfg,
178 LiveVariables::ObserverTy& Obs) {
179 Solver S(*this);
180 ObserverTy* OldObserver = getAnalysisData().Observer;
181 getAnalysisData().Observer = &Obs;
182 S.runOnAllBlocks(cfg);
183 getAnalysisData().Observer = OldObserver;
Ted Kremenek27b07c52007-09-06 21:26:58 +0000184}
185
186//===----------------------------------------------------------------------===//
187// liveness queries
188//
189
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000190bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000191 return getBlockData(B)(D,getAnalysisData());
Ted Kremenek27b07c52007-09-06 21:26:58 +0000192}
193
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000194bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000195 return Live(D,getAnalysisData());
Ted Kremenek055c2752007-09-06 23:00:42 +0000196}
197
Ted Kremenek055c2752007-09-06 23:00:42 +0000198//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000199// printing liveness state for debugging
200//
201
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000202void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
203 const AnalysisDataTy& AD = getAnalysisData();
204
Ted Kremenekf63aa452007-09-28 20:38:59 +0000205 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
206 E = AD.end_decl(); I!=E; ++I)
207 if (V.getDeclBit(I->second)) {
Ted Kremenek27b07c52007-09-06 21:26:58 +0000208 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000209
Ted Kremenek27b07c52007-09-06 21:26:58 +0000210 fprintf(stderr, " %s <%s:%u:%u>\n",
211 I->first->getIdentifier()->getName(),
212 SM.getSourceName(PhysLoc),
213 SM.getLineNumber(PhysLoc),
214 SM.getColumnNumber(PhysLoc));
Ted Kremeneke4e63342007-09-06 00:17:54 +0000215 }
Ted Kremeneke4e63342007-09-06 00:17:54 +0000216}
217
Ted Kremenek27b07c52007-09-06 21:26:58 +0000218void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000219 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
220 E = getBlockDataMap().end(); I!=E; ++I) {
221 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek27b07c52007-09-06 21:26:58 +0000222 I->first->getBlockID());
223
224 dumpLiveness(I->second,M);
225 }
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000226
227 fprintf(stderr,"\n");
228}