blob: eba1a77941f5244ef1683f49cbe2ab25545765c5 [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 "llvm/ADT/SmallPtrSet.h"
22
Ted Kremenek05334682007-09-06 21:26:58 +000023#include <string.h>
24#include <stdio.h>
Ted Kremenekaa04c512007-09-06 00:17:54 +000025
26using namespace clang;
27
28//===----------------------------------------------------------------------===//
Ted Kremenekd7a2f812007-09-25 04:31:27 +000029// Dataflow initialization logic.
30//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +000031
32namespace {
Ted Kremenek8ce772b2007-10-01 20:33:52 +000033class RegisterDecls : public CFGRecStmtDeclVisitor<RegisterDecls> {
Ted Kremenekd7a2f812007-09-25 04:31:27 +000034 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekeebf1312007-09-28 20:38:59 +000035public:
Ted Kremenek8ce772b2007-10-01 20:33:52 +000036 RegisterDecls(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekeebf1312007-09-28 20:38:59 +000037 void VisitVarDecl(VarDecl* VD) { AD.Register(VD); }
Ted Kremenek705386b2007-11-20 03:01:58 +000038 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek8ce772b2007-10-01 20:33:52 +000039};
Ted Kremenekaa04c512007-09-06 00:17:54 +000040} // end anonymous namespace
41
Ted Kremenekd7a2f812007-09-25 04:31:27 +000042void LiveVariables::InitializeValues(const CFG& cfg) {
Ted Kremenek8ce772b2007-10-01 20:33:52 +000043 RegisterDecls R(getAnalysisData());
Ted Kremenekd7a2f812007-09-25 04:31:27 +000044 cfg.VisitBlockStmts(R);
45}
Ted Kremenekaa04c512007-09-06 00:17:54 +000046
47//===----------------------------------------------------------------------===//
Ted Kremenekd7a2f812007-09-25 04:31:27 +000048// Transfer functions.
49//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +000050
51namespace {
Ted Kremenekeebf1312007-09-28 20:38:59 +000052
53static const bool Alive = true;
54static const bool Dead = false;
55
Ted Kremenek705386b2007-11-20 03:01:58 +000056class TransferFuncs : public CFGRecStmtVisitor<TransferFuncs> {
Ted Kremenekd7a2f812007-09-25 04:31:27 +000057 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekeebf1312007-09-28 20:38:59 +000058 LiveVariables::ValTy LiveState;
Ted Kremenekaa04c512007-09-06 00:17:54 +000059public:
Ted Kremenek8ce772b2007-10-01 20:33:52 +000060 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekd7a2f812007-09-25 04:31:27 +000061
Ted Kremenekeebf1312007-09-28 20:38:59 +000062 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek705386b2007-11-20 03:01:58 +000063 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek00b63272007-09-12 19:10:52 +000064
Ted Kremenekaa04c512007-09-06 00:17:54 +000065 void VisitDeclRefExpr(DeclRefExpr* DR);
66 void VisitBinaryOperator(BinaryOperator* B);
67 void VisitAssign(BinaryOperator* B);
Ted Kremenek05334682007-09-06 21:26:58 +000068 void VisitDeclStmt(DeclStmt* DS);
69 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremenek8ce772b2007-10-01 20:33:52 +000070 void Visit(Stmt *S);
Ted Kremenekeebf1312007-09-28 20:38:59 +000071
Ted Kremenek83a6ba12007-09-28 21:29:33 +000072 DeclRefExpr* FindDeclRef(Stmt *S);
Ted Kremenekaa04c512007-09-06 00:17:54 +000073};
Ted Kremenek8ce772b2007-10-01 20:33:52 +000074
Ted Kremenek8ce772b2007-10-01 20:33:52 +000075void TransferFuncs::Visit(Stmt *S) {
76 if (AD.Observer)
77 AD.Observer->ObserveStmt(S,AD,LiveState);
Ted Kremenekeebf1312007-09-28 20:38:59 +000078
Ted Kremenek8ce772b2007-10-01 20:33:52 +000079 static_cast<CFGStmtVisitor<TransferFuncs>*>(this)->Visit(S);
80}
Ted Kremenekaa04c512007-09-06 00:17:54 +000081
Ted Kremenekd7a2f812007-09-25 04:31:27 +000082void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
83 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekeebf1312007-09-28 20:38:59 +000084 LiveState(V,AD) = Alive;
Ted Kremenekaa04c512007-09-06 00:17:54 +000085}
Ted Kremenekaa04c512007-09-06 00:17:54 +000086
Ted Kremenekd7a2f812007-09-25 04:31:27 +000087void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek00b63272007-09-12 19:10:52 +000088 if (B->isAssignmentOp()) VisitAssign(B);
89 else VisitStmt(B);
Ted Kremenekaa04c512007-09-06 00:17:54 +000090}
91
Ted Kremenekd7a2f812007-09-25 04:31:27 +000092void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek83a6ba12007-09-28 21:29:33 +000093 Stmt *S = U->getSubExpr();
94
Ted Kremenek05334682007-09-06 21:26:58 +000095 switch (U->getOpcode()) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +000096 case UnaryOperator::PostInc:
97 case UnaryOperator::PostDec:
98 case UnaryOperator::PreInc:
99 case UnaryOperator::PreDec:
100 case UnaryOperator::AddrOf:
101 // Walk through the subexpressions, blasting through ParenExprs
102 // until we either find a DeclRefExpr or some non-DeclRefExpr
103 // expression.
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000104 if (DeclRefExpr* DR = FindDeclRef(S)) {
105 // Treat the --/++/& operator as a kill.
106 LiveState(DR->getDecl(),AD) = Dead;
107 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
108 return VisitDeclRefExpr(DR);
Ted Kremenekeebf1312007-09-28 20:38:59 +0000109 }
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000110
111 // Fall-through.
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000112
113 default:
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000114 return Visit(S);
Ted Kremenek05334682007-09-06 21:26:58 +0000115 }
116}
117
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000118DeclRefExpr* TransferFuncs::FindDeclRef(Stmt *S) {
119 for (;;)
120 if (ParenExpr* P = dyn_cast<ParenExpr>(S)) {
121 S = P->getSubExpr(); continue;
122 }
123 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
124 return DR;
125 else
126 return NULL;
127}
128
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000129void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000130 Stmt* LHS = B->getLHS();
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000131
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000132 // Assigning to a variable?
133 if (DeclRefExpr* DR = FindDeclRef(LHS)) {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000134 LiveState(DR->getDecl(),AD) = Dead;
135 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
136
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000137 // Handle things like +=, etc., which also generate "uses"
138 // of a variable. Do this just by visiting the subexpression.
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000139 if (B->getOpcode() != BinaryOperator::Assign)
140 VisitDeclRefExpr(DR);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000141 }
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000142 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek05334682007-09-06 21:26:58 +0000143 Visit(LHS);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000144
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000145 Visit(B->getRHS());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000146}
147
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000148void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
149 // Declarations effectively "kill" a variable since they cannot
150 // possibly be live before they are declared.
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000151 for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator())
Ted Kremenekeebf1312007-09-28 20:38:59 +0000152 LiveState(D,AD) = Dead;
Ted Kremenek05334682007-09-06 21:26:58 +0000153}
Ted Kremenekeebf1312007-09-28 20:38:59 +0000154
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}