blob: e59a48859119980b5cfa3eb7144a0e8baaf2d49c [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
Ted Kremenekf41ac5f2008-03-13 16:55:07 +000046LiveVariables::LiveVariables(CFG& cfg) {
47 // Register all referenced VarDecls.
Ted Kremenekd03aece2008-01-29 05:13:23 +000048 getAnalysisData().setCFG(&cfg);
Ted Kremenek8ce772b2007-10-01 20:33:52 +000049 RegisterDecls R(getAnalysisData());
Ted Kremenekd7a2f812007-09-25 04:31:27 +000050 cfg.VisitBlockStmts(R);
51}
Ted Kremenekaa04c512007-09-06 00:17:54 +000052
53//===----------------------------------------------------------------------===//
Ted Kremenekd7a2f812007-09-25 04:31:27 +000054// Transfer functions.
55//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +000056
57namespace {
Ted Kremenekeebf1312007-09-28 20:38:59 +000058
59static const bool Alive = true;
60static const bool Dead = false;
61
Ted Kremenekec818352008-01-08 18:19:08 +000062class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekd7a2f812007-09-25 04:31:27 +000063 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekeebf1312007-09-28 20:38:59 +000064 LiveVariables::ValTy LiveState;
Ted Kremenekaa04c512007-09-06 00:17:54 +000065public:
Ted Kremenek8ce772b2007-10-01 20:33:52 +000066 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekd7a2f812007-09-25 04:31:27 +000067
Ted Kremenekeebf1312007-09-28 20:38:59 +000068 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek705386b2007-11-20 03:01:58 +000069 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek00b63272007-09-12 19:10:52 +000070
Ted Kremenekaa04c512007-09-06 00:17:54 +000071 void VisitDeclRefExpr(DeclRefExpr* DR);
72 void VisitBinaryOperator(BinaryOperator* B);
73 void VisitAssign(BinaryOperator* B);
Ted Kremenek05334682007-09-06 21:26:58 +000074 void VisitDeclStmt(DeclStmt* DS);
75 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremenek8ce772b2007-10-01 20:33:52 +000076 void Visit(Stmt *S);
Ted Kremenekaa04c512007-09-06 00:17:54 +000077};
Ted Kremenek8ce772b2007-10-01 20:33:52 +000078
Ted Kremenek8ce772b2007-10-01 20:33:52 +000079void TransferFuncs::Visit(Stmt *S) {
80 if (AD.Observer)
81 AD.Observer->ObserveStmt(S,AD,LiveState);
Ted Kremenekeebf1312007-09-28 20:38:59 +000082
Ted Kremenekab6c5902008-01-17 20:48:37 +000083 if (S == getCurrentBlkStmt()) {
Ted Kremenek925b3a92008-03-05 19:26:46 +000084 if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
Ted Kremenek1796e132008-01-18 00:40:21 +000085 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenekab6c5902008-01-17 20:48:37 +000086 }
87 else if (!getCFG().isBlkExpr(S))
88 StmtVisitor<TransferFuncs,void>::Visit(S);
89 else
90 // For block-level expressions, mark that they are live.
91 LiveState(S,AD) = Alive;
Ted Kremenek8ce772b2007-10-01 20:33:52 +000092}
Ted Kremenekaa04c512007-09-06 00:17:54 +000093
Ted Kremenekd7a2f812007-09-25 04:31:27 +000094void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
95 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekeebf1312007-09-28 20:38:59 +000096 LiveState(V,AD) = Alive;
Ted Kremenekaa04c512007-09-06 00:17:54 +000097}
Ted Kremenekaa04c512007-09-06 00:17:54 +000098
Ted Kremenekd7a2f812007-09-25 04:31:27 +000099void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek00b63272007-09-12 19:10:52 +0000100 if (B->isAssignmentOp()) VisitAssign(B);
101 else VisitStmt(B);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000102}
103
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000104void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek133befa2008-01-17 17:50:49 +0000105 Expr *E = U->getSubExpr();
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000106
Ted Kremenek05334682007-09-06 21:26:58 +0000107 switch (U->getOpcode()) {
Ted Kremeneke33d1002007-12-13 04:47:15 +0000108 case UnaryOperator::SizeOf: return;
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000109 case UnaryOperator::PostInc:
110 case UnaryOperator::PostDec:
111 case UnaryOperator::PreInc:
112 case UnaryOperator::PreDec:
113 case UnaryOperator::AddrOf:
114 // Walk through the subexpressions, blasting through ParenExprs
115 // until we either find a DeclRefExpr or some non-DeclRefExpr
116 // expression.
Ted Kremenek2c3041c2008-02-22 00:34:10 +0000117 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
118 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
119 // Treat the --/++/& operator as a kill.
120 LiveState(VD, AD) = Dead;
121 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
122 return VisitDeclRefExpr(DR);
123 }
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000124
125 // Fall-through.
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000126
127 default:
Ted Kremenek133befa2008-01-17 17:50:49 +0000128 return Visit(E);
Ted Kremenek05334682007-09-06 21:26:58 +0000129 }
130}
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000131
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000132void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek133befa2008-01-17 17:50:49 +0000133 Expr* LHS = B->getLHS();
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000134
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000135 // Assigning to a variable?
Ted Kremenek133befa2008-01-17 17:50:49 +0000136 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000137 LiveState(DR->getDecl(),AD) = Dead;
138 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
139
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000140 // Handle things like +=, etc., which also generate "uses"
141 // of a variable. Do this just by visiting the subexpression.
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000142 if (B->getOpcode() != BinaryOperator::Assign)
143 VisitDeclRefExpr(DR);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000144 }
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000145 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek05334682007-09-06 21:26:58 +0000146 Visit(LHS);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000147
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000148 Visit(B->getRHS());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000149}
150
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000151void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
152 // Declarations effectively "kill" a variable since they cannot
153 // possibly be live before they are declared.
Ted Kremenek43c51bd2008-02-25 22:28:54 +0000154 for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator())
155 if (VarDecl* VD = dyn_cast<VarDecl>(D)) {
156 LiveState(D,AD) = Dead;
157
Ted Kremenek0c5fe982008-02-07 02:38:55 +0000158 if (Expr* Init = VD->getInit())
159 Visit(Init);
Ted Kremenek43c51bd2008-02-25 22:28:54 +0000160 }
Ted Kremenek05334682007-09-06 21:26:58 +0000161}
Ted Kremenekeebf1312007-09-28 20:38:59 +0000162
Ted Kremenekaa04c512007-09-06 00:17:54 +0000163} // end anonymous namespace
164
Ted Kremenekeebf1312007-09-28 20:38:59 +0000165//===----------------------------------------------------------------------===//
166// Merge operator: if something is live on any successor block, it is live
167// in the current block (a set union).
168//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000169
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000170namespace {
Ted Kremenek58ba7d52008-02-22 23:17:20 +0000171typedef ExprDeclBitVector_Types::Union Merge;
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000172typedef DataflowSolver<LiveVariables,TransferFuncs,Merge> Solver;
Ted Kremenekeebf1312007-09-28 20:38:59 +0000173} // end anonymous namespace
174
175//===----------------------------------------------------------------------===//
176// External interface to run Liveness analysis.
177//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000178
Ted Kremenek5ee98a72008-01-11 00:40:29 +0000179void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000180 Solver S(*this);
181 S.runOnCFG(cfg);
182}
Ted Kremenek05334682007-09-06 21:26:58 +0000183
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000184void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenekab8ed952008-01-17 18:25:22 +0000185 LiveVariables::ObserverTy* Obs,
186 bool recordStmtValues) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000187 Solver S(*this);
188 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenekab8ed952008-01-17 18:25:22 +0000189 getAnalysisData().Observer = Obs;
190 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000191 getAnalysisData().Observer = OldObserver;
Ted Kremenek05334682007-09-06 21:26:58 +0000192}
193
194//===----------------------------------------------------------------------===//
195// liveness queries
196//
197
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000198bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000199 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
200 return i.isValid() ? getBlockData(B).getBit(i) : false;
Ted Kremenek05334682007-09-06 21:26:58 +0000201}
202
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000203bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000204 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
205 return i.isValid() ? Live.getBit(i) : false;
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000206}
207
Ted Kremenekab6c5902008-01-17 20:48:37 +0000208bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
209 return getStmtData(Loc)(StmtVal,getAnalysisData());
210}
211
Ted Kremenek1796e132008-01-18 00:40:21 +0000212bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
213 return getStmtData(Loc)(D,getAnalysisData());
214}
215
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000216//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000217// printing liveness state for debugging
218//
219
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000220void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
221 const AnalysisDataTy& AD = getAnalysisData();
222
Ted Kremenekeebf1312007-09-28 20:38:59 +0000223 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
224 E = AD.end_decl(); I!=E; ++I)
225 if (V.getDeclBit(I->second)) {
Ted Kremenek05334682007-09-06 21:26:58 +0000226 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000227
Ted Kremenek05334682007-09-06 21:26:58 +0000228 fprintf(stderr, " %s <%s:%u:%u>\n",
229 I->first->getIdentifier()->getName(),
230 SM.getSourceName(PhysLoc),
231 SM.getLineNumber(PhysLoc),
232 SM.getColumnNumber(PhysLoc));
Ted Kremenekaa04c512007-09-06 00:17:54 +0000233 }
Ted Kremenekaa04c512007-09-06 00:17:54 +0000234}
235
Ted Kremenek05334682007-09-06 21:26:58 +0000236void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000237 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
238 E = getBlockDataMap().end(); I!=E; ++I) {
239 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek05334682007-09-06 21:26:58 +0000240 I->first->getBlockID());
241
242 dumpLiveness(I->second,M);
243 }
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000244
245 fprintf(stderr,"\n");
246}