blob: d40941d13b789889cd50a3129122d1fa050b3eac [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//
5// This file was developed by Ted Kremenek and is distributed under
Ted Kremenekd7a2f812007-09-25 04:31:27 +00006// the University of Illinois Open Source License. See LICENSE.TXT for
7// 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
15#include "clang/Analysis/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 "clang/Lex/IdentifierTable.h"
22#include "llvm/ADT/SmallPtrSet.h"
23
Ted Kremenek05334682007-09-06 21:26:58 +000024#include <string.h>
25#include <stdio.h>
Ted Kremenekaa04c512007-09-06 00:17:54 +000026
27using namespace clang;
28
29//===----------------------------------------------------------------------===//
Ted Kremenekd7a2f812007-09-25 04:31:27 +000030// Dataflow initialization logic.
31//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +000032
33namespace {
Ted Kremenekeebf1312007-09-28 20:38:59 +000034class RegisterDeclsExprs : public CFGRecStmtDeclVisitor<RegisterDeclsExprs> {
Ted Kremenekd7a2f812007-09-25 04:31:27 +000035 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekeebf1312007-09-28 20:38:59 +000036public:
37 RegisterDeclsExprs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
38
39 void VisitVarDecl(VarDecl* VD) { AD.Register(VD); }
40 void BlockStmt_VisitExpr(Expr* E) { AD.Register(E); }
Ted Kremenekd7a2f812007-09-25 04:31:27 +000041};
Ted Kremenekaa04c512007-09-06 00:17:54 +000042} // end anonymous namespace
43
Ted Kremenekd7a2f812007-09-25 04:31:27 +000044void LiveVariables::InitializeValues(const CFG& cfg) {
Ted Kremenekeebf1312007-09-28 20:38:59 +000045 RegisterDeclsExprs R(getAnalysisData());
Ted Kremenekd7a2f812007-09-25 04:31:27 +000046 cfg.VisitBlockStmts(R);
47}
Ted Kremenekaa04c512007-09-06 00:17:54 +000048
49//===----------------------------------------------------------------------===//
Ted Kremenekd7a2f812007-09-25 04:31:27 +000050// Transfer functions.
51//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +000052
53namespace {
Ted Kremenekeebf1312007-09-28 20:38:59 +000054
55static const bool Alive = true;
56static const bool Dead = false;
57
Ted Kremenekd7a2f812007-09-25 04:31:27 +000058class TransferFuncs : public CFGStmtVisitor<TransferFuncs> {
59 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekeebf1312007-09-28 20:38:59 +000060 LiveVariables::ValTy LiveState;
61 bool ExprLiveness;
Ted Kremenekaa04c512007-09-06 00:17:54 +000062public:
Ted Kremenekeebf1312007-09-28 20:38:59 +000063 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad),
64 ExprLiveness(Dead) {}
Ted Kremenekd7a2f812007-09-25 04:31:27 +000065
Ted Kremenekeebf1312007-09-28 20:38:59 +000066 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek00b63272007-09-12 19:10:52 +000067
Ted Kremenekaa04c512007-09-06 00:17:54 +000068 void VisitDeclRefExpr(DeclRefExpr* DR);
69 void VisitBinaryOperator(BinaryOperator* B);
70 void VisitAssign(BinaryOperator* B);
Ted Kremenek05334682007-09-06 21:26:58 +000071 void VisitDeclStmt(DeclStmt* DS);
72 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremenekeebf1312007-09-28 20:38:59 +000073 void VisitStmt(Stmt* S);
74 void BlockStmt_VisitExpr(Expr *E);
75
Ted Kremenekd7a2f812007-09-25 04:31:27 +000076 void Visit(Stmt *S) {
Ted Kremenekeebf1312007-09-28 20:38:59 +000077 if (AD.Observer) AD.Observer->ObserveStmt(S,AD,LiveState);
Ted Kremenekd7a2f812007-09-25 04:31:27 +000078 static_cast<CFGStmtVisitor<TransferFuncs>*>(this)->Visit(S);
Ted Kremenekaa04c512007-09-06 00:17:54 +000079 }
Ted Kremenekaa04c512007-09-06 00:17:54 +000080};
Ted Kremenekeebf1312007-09-28 20:38:59 +000081
Ted Kremenekaa04c512007-09-06 00:17:54 +000082
Ted Kremenekd7a2f812007-09-25 04:31:27 +000083void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
84 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekeebf1312007-09-28 20:38:59 +000085 LiveState(V,AD) = Alive;
Ted Kremenekaa04c512007-09-06 00:17:54 +000086}
Ted Kremenekaa04c512007-09-06 00:17:54 +000087
Ted Kremenekd7a2f812007-09-25 04:31:27 +000088void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek00b63272007-09-12 19:10:52 +000089 if (B->isAssignmentOp()) VisitAssign(B);
90 else VisitStmt(B);
Ted Kremenekaa04c512007-09-06 00:17:54 +000091}
92
Ted Kremenekd7a2f812007-09-25 04:31:27 +000093void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek05334682007-09-06 21:26:58 +000094 switch (U->getOpcode()) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +000095 case UnaryOperator::PostInc:
96 case UnaryOperator::PostDec:
97 case UnaryOperator::PreInc:
98 case UnaryOperator::PreDec:
99 case UnaryOperator::AddrOf:
100 // Walk through the subexpressions, blasting through ParenExprs
101 // until we either find a DeclRefExpr or some non-DeclRefExpr
102 // expression.
103 for (Stmt* S = U->getSubExpr() ;;) {
104 if (ParenExpr* P = dyn_cast<ParenExpr>(S)) { S=P->getSubExpr(); continue;}
105 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) {
106 // Treat the --/++/& operator as a kill.
Ted Kremenekeebf1312007-09-28 20:38:59 +0000107 LiveState(DR->getDecl(),AD) = Dead;
108 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
109 return VisitDeclRefExpr(DR);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000110 }
Ted Kremenekeebf1312007-09-28 20:38:59 +0000111 else return Visit(S);
112 }
113
114 assert (false && "Unreachable.");
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000115
116 default:
Ted Kremenekeebf1312007-09-28 20:38:59 +0000117 return Visit(U->getSubExpr());
Ted Kremenek05334682007-09-06 21:26:58 +0000118 }
119}
120
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000121void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000122 Stmt* LHS = B->getLHS();
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000123
Ted Kremenekeebf1312007-09-28 20:38:59 +0000124 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) { // Assigning to a var?
125 LiveState(DR->getDecl(),AD) = Dead;
126 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
127
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000128 // Handle things like +=, etc., which also generate "uses"
129 // of a variable. Do this just by visiting the subexpression.
130 if (B->getOpcode() != BinaryOperator::Assign) Visit(LHS);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000131 }
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000132 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek05334682007-09-06 21:26:58 +0000133 Visit(LHS);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000134
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000135 Visit(B->getRHS());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000136}
137
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000138void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
139 // Declarations effectively "kill" a variable since they cannot
140 // possibly be live before they are declared.
Steve Naroff2591e1b2007-09-13 23:52:58 +0000141 for (ScopedDecl* D = DS->getDecl(); D != NULL ; D = D->getNextDeclarator())
Ted Kremenekeebf1312007-09-28 20:38:59 +0000142 LiveState(D,AD) = Dead;
Ted Kremenek05334682007-09-06 21:26:58 +0000143}
Ted Kremenekeebf1312007-09-28 20:38:59 +0000144
145void TransferFuncs::VisitStmt(Stmt* S) {
146 if (AD.isTracked(static_cast<Expr*>(S))) return;
147 else VisitChildren(S);
148}
149
150void TransferFuncs::BlockStmt_VisitExpr(Expr* E) {
151 assert (AD.isTracked(E));
152 static_cast<CFGStmtVisitor<TransferFuncs>*>(this)->Visit(E);
153}
154
Ted Kremenekaa04c512007-09-06 00:17:54 +0000155} // end anonymous namespace
156
Ted Kremenekeebf1312007-09-28 20:38:59 +0000157//===----------------------------------------------------------------------===//
158// Merge operator: if something is live on any successor block, it is live
159// in the current block (a set union).
160//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000161
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000162namespace {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000163typedef DeclBitVector_Types::Union Merge;
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000164typedef DataflowSolver<LiveVariables,TransferFuncs,Merge> Solver;
Ted Kremenekeebf1312007-09-28 20:38:59 +0000165} // end anonymous namespace
166
167//===----------------------------------------------------------------------===//
168// External interface to run Liveness analysis.
169//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000170
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000171void LiveVariables::runOnCFG(const CFG& cfg) {
172 Solver S(*this);
173 S.runOnCFG(cfg);
174}
Ted Kremenek05334682007-09-06 21:26:58 +0000175
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000176void LiveVariables::runOnAllBlocks(const CFG& cfg,
177 LiveVariables::ObserverTy& Obs) {
178 Solver S(*this);
179 ObserverTy* OldObserver = getAnalysisData().Observer;
180 getAnalysisData().Observer = &Obs;
181 S.runOnAllBlocks(cfg);
182 getAnalysisData().Observer = OldObserver;
Ted Kremenek05334682007-09-06 21:26:58 +0000183}
184
185//===----------------------------------------------------------------------===//
186// liveness queries
187//
188
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000189bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000190 return getBlockData(B)(D,getAnalysisData());
Ted Kremenek05334682007-09-06 21:26:58 +0000191}
192
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000193bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000194 return Live(D,getAnalysisData());
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000195}
196
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000197//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000198// printing liveness state for debugging
199//
200
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000201void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
202 const AnalysisDataTy& AD = getAnalysisData();
203
Ted Kremenekeebf1312007-09-28 20:38:59 +0000204 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
205 E = AD.end_decl(); I!=E; ++I)
206 if (V.getDeclBit(I->second)) {
Ted Kremenek05334682007-09-06 21:26:58 +0000207 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000208
Ted Kremenek05334682007-09-06 21:26:58 +0000209 fprintf(stderr, " %s <%s:%u:%u>\n",
210 I->first->getIdentifier()->getName(),
211 SM.getSourceName(PhysLoc),
212 SM.getLineNumber(PhysLoc),
213 SM.getColumnNumber(PhysLoc));
Ted Kremenekaa04c512007-09-06 00:17:54 +0000214 }
Ted Kremenekaa04c512007-09-06 00:17:54 +0000215}
216
Ted Kremenek05334682007-09-06 21:26:58 +0000217void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000218 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
219 E = getBlockDataMap().end(); I!=E; ++I) {
220 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek05334682007-09-06 21:26:58 +0000221 I->first->getBlockID());
222
223 dumpLiveness(I->second,M);
224 }
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000225
226 fprintf(stderr,"\n");
227}