blob: 9515cb8eb715b79e4a654ce6f47188ca78b1cbf8 [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//
Chris Lattner959e5be2007-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 Kremenekd7a2f812007-09-25 04:31:27 +00007// 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
Ted Kremenekcdf8e842007-12-21 21:42:19 +000015#include "clang/Analysis/Analyses/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 "llvm/ADT/SmallPtrSet.h"
Ted Kremenekec818352008-01-08 18:19:08 +000022#include "llvm/Support/Compiler.h"
Ted Kremenekaa04c512007-09-06 00:17:54 +000023
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 Kremenekec818352008-01-08 18:19:08 +000034class VISIBILITY_HIDDEN RegisterDecls
35 : public CFGRecStmtDeclVisitor<RegisterDecls> {
36
Ted Kremenekd7a2f812007-09-25 04:31:27 +000037 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekeebf1312007-09-28 20:38:59 +000038public:
Ted Kremenek8ce772b2007-10-01 20:33:52 +000039 RegisterDecls(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekeebf1312007-09-28 20:38:59 +000040 void VisitVarDecl(VarDecl* VD) { AD.Register(VD); }
Ted Kremenek705386b2007-11-20 03:01:58 +000041 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek8ce772b2007-10-01 20:33:52 +000042};
Ted Kremenekaa04c512007-09-06 00:17:54 +000043} // end anonymous namespace
44
Ted Kremenekd03aece2008-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 Kremenek8ce772b2007-10-01 20:33:52 +000054 RegisterDecls R(getAnalysisData());
Ted Kremenekd7a2f812007-09-25 04:31:27 +000055 cfg.VisitBlockStmts(R);
56}
Ted Kremenekaa04c512007-09-06 00:17:54 +000057
58//===----------------------------------------------------------------------===//
Ted Kremenekd7a2f812007-09-25 04:31:27 +000059// Transfer functions.
60//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +000061
62namespace {
Ted Kremenekeebf1312007-09-28 20:38:59 +000063
64static const bool Alive = true;
65static const bool Dead = false;
66
Ted Kremenekec818352008-01-08 18:19:08 +000067class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekd7a2f812007-09-25 04:31:27 +000068 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekeebf1312007-09-28 20:38:59 +000069 LiveVariables::ValTy LiveState;
Ted Kremenekaa04c512007-09-06 00:17:54 +000070public:
Ted Kremenek8ce772b2007-10-01 20:33:52 +000071 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekd7a2f812007-09-25 04:31:27 +000072
Ted Kremenekeebf1312007-09-28 20:38:59 +000073 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek705386b2007-11-20 03:01:58 +000074 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek00b63272007-09-12 19:10:52 +000075
Ted Kremenekaa04c512007-09-06 00:17:54 +000076 void VisitDeclRefExpr(DeclRefExpr* DR);
77 void VisitBinaryOperator(BinaryOperator* B);
78 void VisitAssign(BinaryOperator* B);
Ted Kremenek05334682007-09-06 21:26:58 +000079 void VisitDeclStmt(DeclStmt* DS);
80 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremenek8ce772b2007-10-01 20:33:52 +000081 void Visit(Stmt *S);
Ted Kremenekaa04c512007-09-06 00:17:54 +000082};
Ted Kremenek8ce772b2007-10-01 20:33:52 +000083
Ted Kremenek8ce772b2007-10-01 20:33:52 +000084void TransferFuncs::Visit(Stmt *S) {
85 if (AD.Observer)
86 AD.Observer->ObserveStmt(S,AD,LiveState);
Ted Kremenekeebf1312007-09-28 20:38:59 +000087
Ted Kremenekab6c5902008-01-17 20:48:37 +000088 if (S == getCurrentBlkStmt()) {
Ted Kremenek6d7b8012008-03-05 19:08:15 +000089 assert (!isa<Expr>(S) || getCFG().isBlkExpr(S));
90 if (isa<Expr>(S)) LiveState(S,AD) = Dead;
Ted Kremenek1796e132008-01-18 00:40:21 +000091 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenekab6c5902008-01-17 20:48:37 +000092 }
93 else if (!getCFG().isBlkExpr(S))
94 StmtVisitor<TransferFuncs,void>::Visit(S);
95 else
96 // For block-level expressions, mark that they are live.
97 LiveState(S,AD) = Alive;
Ted Kremenek8ce772b2007-10-01 20:33:52 +000098}
Ted Kremenekaa04c512007-09-06 00:17:54 +000099
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000100void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
101 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekeebf1312007-09-28 20:38:59 +0000102 LiveState(V,AD) = Alive;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000103}
Ted Kremenekaa04c512007-09-06 00:17:54 +0000104
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000105void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek00b63272007-09-12 19:10:52 +0000106 if (B->isAssignmentOp()) VisitAssign(B);
107 else VisitStmt(B);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000108}
109
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000110void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek133befa2008-01-17 17:50:49 +0000111 Expr *E = U->getSubExpr();
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000112
Ted Kremenek05334682007-09-06 21:26:58 +0000113 switch (U->getOpcode()) {
Ted Kremeneke33d1002007-12-13 04:47:15 +0000114 case UnaryOperator::SizeOf: return;
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000115 case UnaryOperator::PostInc:
116 case UnaryOperator::PostDec:
117 case UnaryOperator::PreInc:
118 case UnaryOperator::PreDec:
119 case UnaryOperator::AddrOf:
120 // Walk through the subexpressions, blasting through ParenExprs
121 // until we either find a DeclRefExpr or some non-DeclRefExpr
122 // expression.
Ted Kremenek2c3041c2008-02-22 00:34:10 +0000123 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
124 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
125 // Treat the --/++/& operator as a kill.
126 LiveState(VD, AD) = Dead;
127 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
128 return VisitDeclRefExpr(DR);
129 }
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000130
131 // Fall-through.
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000132
133 default:
Ted Kremenek133befa2008-01-17 17:50:49 +0000134 return Visit(E);
Ted Kremenek05334682007-09-06 21:26:58 +0000135 }
136}
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000137
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000138void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek133befa2008-01-17 17:50:49 +0000139 Expr* LHS = B->getLHS();
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000140
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000141 // Assigning to a variable?
Ted Kremenek133befa2008-01-17 17:50:49 +0000142 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000143 LiveState(DR->getDecl(),AD) = Dead;
144 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
145
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000146 // Handle things like +=, etc., which also generate "uses"
147 // of a variable. Do this just by visiting the subexpression.
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000148 if (B->getOpcode() != BinaryOperator::Assign)
149 VisitDeclRefExpr(DR);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000150 }
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000151 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek05334682007-09-06 21:26:58 +0000152 Visit(LHS);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000153
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000154 Visit(B->getRHS());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000155}
156
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000157void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
158 // Declarations effectively "kill" a variable since they cannot
159 // possibly be live before they are declared.
Ted Kremenek43c51bd2008-02-25 22:28:54 +0000160 for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator())
161 if (VarDecl* VD = dyn_cast<VarDecl>(D)) {
162 LiveState(D,AD) = Dead;
163
Ted Kremenek0c5fe982008-02-07 02:38:55 +0000164 if (Expr* Init = VD->getInit())
165 Visit(Init);
Ted Kremenek43c51bd2008-02-25 22:28:54 +0000166 }
Ted Kremenek05334682007-09-06 21:26:58 +0000167}
Ted Kremenekeebf1312007-09-28 20:38:59 +0000168
Ted Kremenekaa04c512007-09-06 00:17:54 +0000169} // end anonymous namespace
170
Ted Kremenekeebf1312007-09-28 20:38:59 +0000171//===----------------------------------------------------------------------===//
172// Merge operator: if something is live on any successor block, it is live
173// in the current block (a set union).
174//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000175
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000176namespace {
Ted Kremenek58ba7d52008-02-22 23:17:20 +0000177typedef ExprDeclBitVector_Types::Union Merge;
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000178typedef DataflowSolver<LiveVariables,TransferFuncs,Merge> Solver;
Ted Kremenekeebf1312007-09-28 20:38:59 +0000179} // end anonymous namespace
180
181//===----------------------------------------------------------------------===//
182// External interface to run Liveness analysis.
183//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000184
Ted Kremenek5ee98a72008-01-11 00:40:29 +0000185void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000186 Solver S(*this);
187 S.runOnCFG(cfg);
188}
Ted Kremenek05334682007-09-06 21:26:58 +0000189
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000190void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenekab8ed952008-01-17 18:25:22 +0000191 LiveVariables::ObserverTy* Obs,
192 bool recordStmtValues) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000193 Solver S(*this);
194 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenekab8ed952008-01-17 18:25:22 +0000195 getAnalysisData().Observer = Obs;
196 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000197 getAnalysisData().Observer = OldObserver;
Ted Kremenek05334682007-09-06 21:26:58 +0000198}
199
200//===----------------------------------------------------------------------===//
201// liveness queries
202//
203
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000204bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000205 return getBlockData(B)(D,getAnalysisData());
Ted Kremenek05334682007-09-06 21:26:58 +0000206}
207
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000208bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000209 return Live(D,getAnalysisData());
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000210}
211
Ted Kremenekab6c5902008-01-17 20:48:37 +0000212bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
213 return getStmtData(Loc)(StmtVal,getAnalysisData());
214}
215
Ted Kremenek1796e132008-01-18 00:40:21 +0000216bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
217 return getStmtData(Loc)(D,getAnalysisData());
218}
219
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000220//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000221// printing liveness state for debugging
222//
223
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000224void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
225 const AnalysisDataTy& AD = getAnalysisData();
226
Ted Kremenekeebf1312007-09-28 20:38:59 +0000227 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
228 E = AD.end_decl(); I!=E; ++I)
229 if (V.getDeclBit(I->second)) {
Ted Kremenek05334682007-09-06 21:26:58 +0000230 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000231
Ted Kremenek05334682007-09-06 21:26:58 +0000232 fprintf(stderr, " %s <%s:%u:%u>\n",
233 I->first->getIdentifier()->getName(),
234 SM.getSourceName(PhysLoc),
235 SM.getLineNumber(PhysLoc),
236 SM.getColumnNumber(PhysLoc));
Ted Kremenekaa04c512007-09-06 00:17:54 +0000237 }
Ted Kremenekaa04c512007-09-06 00:17:54 +0000238}
239
Ted Kremenek05334682007-09-06 21:26:58 +0000240void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000241 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
242 E = getBlockDataMap().end(); I!=E; ++I) {
243 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek05334682007-09-06 21:26:58 +0000244 I->first->getBlockID());
245
246 dumpLiveness(I->second,M);
247 }
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000248
249 fprintf(stderr,"\n");
250}