blob: 92186537c7b16a584c454d0704636d943e1aa1af [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 Kremenekd7a2f812007-09-25 04:31:27 +000045void LiveVariables::InitializeValues(const CFG& cfg) {
Ted Kremenek8ce772b2007-10-01 20:33:52 +000046 RegisterDecls R(getAnalysisData());
Ted Kremenekd7a2f812007-09-25 04:31:27 +000047 cfg.VisitBlockStmts(R);
48}
Ted Kremenekaa04c512007-09-06 00:17:54 +000049
50//===----------------------------------------------------------------------===//
Ted Kremenekd7a2f812007-09-25 04:31:27 +000051// Transfer functions.
52//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +000053
54namespace {
Ted Kremenekeebf1312007-09-28 20:38:59 +000055
56static const bool Alive = true;
57static const bool Dead = false;
58
Ted Kremenekec818352008-01-08 18:19:08 +000059class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekd7a2f812007-09-25 04:31:27 +000060 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekeebf1312007-09-28 20:38:59 +000061 LiveVariables::ValTy LiveState;
Ted Kremenekaa04c512007-09-06 00:17:54 +000062public:
Ted Kremenek8ce772b2007-10-01 20:33:52 +000063 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekd7a2f812007-09-25 04:31:27 +000064
Ted Kremenekeebf1312007-09-28 20:38:59 +000065 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek705386b2007-11-20 03:01:58 +000066 CFG& getCFG() { return AD.getCFG(); }
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 Kremenek8ce772b2007-10-01 20:33:52 +000073 void Visit(Stmt *S);
Ted Kremenekeebf1312007-09-28 20:38:59 +000074
Ted Kremenek83a6ba12007-09-28 21:29:33 +000075 DeclRefExpr* FindDeclRef(Stmt *S);
Ted Kremenekaa04c512007-09-06 00:17:54 +000076};
Ted Kremenek8ce772b2007-10-01 20:33:52 +000077
Ted Kremenek8ce772b2007-10-01 20:33:52 +000078void TransferFuncs::Visit(Stmt *S) {
79 if (AD.Observer)
80 AD.Observer->ObserveStmt(S,AD,LiveState);
Ted Kremenekeebf1312007-09-28 20:38:59 +000081
Ted Kremenek8ce772b2007-10-01 20:33:52 +000082 static_cast<CFGStmtVisitor<TransferFuncs>*>(this)->Visit(S);
83}
Ted Kremenekaa04c512007-09-06 00:17:54 +000084
Ted Kremenekd7a2f812007-09-25 04:31:27 +000085void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
86 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekeebf1312007-09-28 20:38:59 +000087 LiveState(V,AD) = Alive;
Ted Kremenekaa04c512007-09-06 00:17:54 +000088}
Ted Kremenekaa04c512007-09-06 00:17:54 +000089
Ted Kremenekd7a2f812007-09-25 04:31:27 +000090void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek00b63272007-09-12 19:10:52 +000091 if (B->isAssignmentOp()) VisitAssign(B);
92 else VisitStmt(B);
Ted Kremenekaa04c512007-09-06 00:17:54 +000093}
94
Ted Kremenekd7a2f812007-09-25 04:31:27 +000095void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek83a6ba12007-09-28 21:29:33 +000096 Stmt *S = U->getSubExpr();
97
Ted Kremenek05334682007-09-06 21:26:58 +000098 switch (U->getOpcode()) {
Ted Kremeneke33d1002007-12-13 04:47:15 +000099 case UnaryOperator::SizeOf: return;
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000100 case UnaryOperator::PostInc:
101 case UnaryOperator::PostDec:
102 case UnaryOperator::PreInc:
103 case UnaryOperator::PreDec:
104 case UnaryOperator::AddrOf:
105 // Walk through the subexpressions, blasting through ParenExprs
106 // until we either find a DeclRefExpr or some non-DeclRefExpr
107 // expression.
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000108 if (DeclRefExpr* DR = FindDeclRef(S)) {
109 // Treat the --/++/& operator as a kill.
110 LiveState(DR->getDecl(),AD) = Dead;
111 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
112 return VisitDeclRefExpr(DR);
Ted Kremenekeebf1312007-09-28 20:38:59 +0000113 }
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000114
115 // Fall-through.
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000116
117 default:
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000118 return Visit(S);
Ted Kremenek05334682007-09-06 21:26:58 +0000119 }
120}
121
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000122DeclRefExpr* TransferFuncs::FindDeclRef(Stmt *S) {
123 for (;;)
124 if (ParenExpr* P = dyn_cast<ParenExpr>(S)) {
125 S = P->getSubExpr(); continue;
126 }
127 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S))
128 return DR;
129 else
130 return NULL;
131}
132
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000133void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000134 Stmt* LHS = B->getLHS();
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000135
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000136 // Assigning to a variable?
137 if (DeclRefExpr* DR = FindDeclRef(LHS)) {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000138 LiveState(DR->getDecl(),AD) = Dead;
139 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
140
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000141 // Handle things like +=, etc., which also generate "uses"
142 // of a variable. Do this just by visiting the subexpression.
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000143 if (B->getOpcode() != BinaryOperator::Assign)
144 VisitDeclRefExpr(DR);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000145 }
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000146 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek05334682007-09-06 21:26:58 +0000147 Visit(LHS);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000148
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000149 Visit(B->getRHS());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000150}
151
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000152void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
153 // Declarations effectively "kill" a variable since they cannot
154 // possibly be live before they are declared.
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000155 for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator())
Ted Kremenekeebf1312007-09-28 20:38:59 +0000156 LiveState(D,AD) = Dead;
Ted Kremenek05334682007-09-06 21:26:58 +0000157}
Ted Kremenekeebf1312007-09-28 20:38:59 +0000158
Ted Kremenekaa04c512007-09-06 00:17:54 +0000159} // end anonymous namespace
160
Ted Kremenekeebf1312007-09-28 20:38:59 +0000161//===----------------------------------------------------------------------===//
162// Merge operator: if something is live on any successor block, it is live
163// in the current block (a set union).
164//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000165
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000166namespace {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000167typedef DeclBitVector_Types::Union Merge;
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000168typedef DataflowSolver<LiveVariables,TransferFuncs,Merge> Solver;
Ted Kremenekeebf1312007-09-28 20:38:59 +0000169} // end anonymous namespace
170
171//===----------------------------------------------------------------------===//
172// External interface to run Liveness analysis.
173//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000174
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000175void LiveVariables::runOnCFG(const CFG& cfg) {
176 Solver S(*this);
177 S.runOnCFG(cfg);
178}
Ted Kremenek05334682007-09-06 21:26:58 +0000179
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000180void LiveVariables::runOnAllBlocks(const CFG& cfg,
181 LiveVariables::ObserverTy& Obs) {
182 Solver S(*this);
183 ObserverTy* OldObserver = getAnalysisData().Observer;
184 getAnalysisData().Observer = &Obs;
185 S.runOnAllBlocks(cfg);
186 getAnalysisData().Observer = OldObserver;
Ted Kremenek05334682007-09-06 21:26:58 +0000187}
188
189//===----------------------------------------------------------------------===//
190// liveness queries
191//
192
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000193bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000194 return getBlockData(B)(D,getAnalysisData());
Ted Kremenek05334682007-09-06 21:26:58 +0000195}
196
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000197bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000198 return Live(D,getAnalysisData());
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000199}
200
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000201//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000202// printing liveness state for debugging
203//
204
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000205void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
206 const AnalysisDataTy& AD = getAnalysisData();
207
Ted Kremenekeebf1312007-09-28 20:38:59 +0000208 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
209 E = AD.end_decl(); I!=E; ++I)
210 if (V.getDeclBit(I->second)) {
Ted Kremenek05334682007-09-06 21:26:58 +0000211 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000212
Ted Kremenek05334682007-09-06 21:26:58 +0000213 fprintf(stderr, " %s <%s:%u:%u>\n",
214 I->first->getIdentifier()->getName(),
215 SM.getSourceName(PhysLoc),
216 SM.getLineNumber(PhysLoc),
217 SM.getColumnNumber(PhysLoc));
Ted Kremenekaa04c512007-09-06 00:17:54 +0000218 }
Ted Kremenekaa04c512007-09-06 00:17:54 +0000219}
220
Ted Kremenek05334682007-09-06 21:26:58 +0000221void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000222 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
223 E = getBlockDataMap().end(); I!=E; ++I) {
224 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek05334682007-09-06 21:26:58 +0000225 I->first->getBlockID());
226
227 dumpLiveness(I->second,M);
228 }
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000229
230 fprintf(stderr,"\n");
231}