blob: b704b2d8267ccfe199dfc0d8499f7d9887583aad [file] [log] [blame]
Ted Kremenek24c62442007-09-20 21:42:55 +00001//=- LiveVariables.cpp - Live Variable Analysis for Source CFGs -*- C++ --*-==//
Ted Kremenekb56a9902007-09-06 00:17:54 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Kremenekad8bce02007-09-25 04:31:27 +00007// details.
Ted Kremenekb56a9902007-09-06 00:17:54 +00008//
9//===----------------------------------------------------------------------===//
10//
11// This file implements Live Variables analysis for source-level CFGs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekbf593f82007-12-21 21:42:19 +000015#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek3f8ed262007-09-06 21:26:58 +000016#include "clang/Basic/SourceManager.h"
Ted Kremenekb56a9902007-09-06 00:17:54 +000017#include "clang/AST/Expr.h"
18#include "clang/AST/CFG.h"
Ted Kremenekad8bce02007-09-25 04:31:27 +000019#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenek39fc60f2007-09-25 21:00:24 +000020#include "clang/Analysis/FlowSensitive/DataflowSolver.h"
Ted Kremenekb56a9902007-09-06 00:17:54 +000021#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek96b1ce42008-01-08 18:19:08 +000022#include "llvm/Support/Compiler.h"
Ted Kremenekb56a9902007-09-06 00:17:54 +000023
Ted Kremenek3f8ed262007-09-06 21:26:58 +000024#include <string.h>
25#include <stdio.h>
Ted Kremenekb56a9902007-09-06 00:17:54 +000026
27using namespace clang;
28
29//===----------------------------------------------------------------------===//
Ted Kremenekad8bce02007-09-25 04:31:27 +000030// Dataflow initialization logic.
31//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +000032
33namespace {
Ted Kremenek96b1ce42008-01-08 18:19:08 +000034class VISIBILITY_HIDDEN RegisterDecls
35 : public CFGRecStmtDeclVisitor<RegisterDecls> {
36
Ted Kremenekad8bce02007-09-25 04:31:27 +000037 LiveVariables::AnalysisDataTy& AD;
Ted Kremenek0064ff42007-09-28 20:38:59 +000038public:
Ted Kremenekfb4750b2007-10-01 20:33:52 +000039 RegisterDecls(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenek0064ff42007-09-28 20:38:59 +000040 void VisitVarDecl(VarDecl* VD) { AD.Register(VD); }
Ted Kremenek9d0acca2007-11-20 03:01:58 +000041 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenekfb4750b2007-10-01 20:33:52 +000042};
Ted Kremenekb56a9902007-09-06 00:17:54 +000043} // end anonymous namespace
44
Ted Kremenek8ff7705a2008-01-29 05:13:23 +000045
Ted Kremenek1fdd0a42008-03-13 16:55:07 +000046LiveVariables::LiveVariables(CFG& cfg) {
47 // Register all referenced VarDecls.
Ted Kremenek8ff7705a2008-01-29 05:13:23 +000048 getAnalysisData().setCFG(&cfg);
Ted Kremenekfb4750b2007-10-01 20:33:52 +000049 RegisterDecls R(getAnalysisData());
Ted Kremenekad8bce02007-09-25 04:31:27 +000050 cfg.VisitBlockStmts(R);
51}
Ted Kremenekb56a9902007-09-06 00:17:54 +000052
53//===----------------------------------------------------------------------===//
Ted Kremenekad8bce02007-09-25 04:31:27 +000054// Transfer functions.
55//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +000056
57namespace {
Ted Kremenek0064ff42007-09-28 20:38:59 +000058
59static const bool Alive = true;
60static const bool Dead = false;
61
Ted Kremenek96b1ce42008-01-08 18:19:08 +000062class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekad8bce02007-09-25 04:31:27 +000063 LiveVariables::AnalysisDataTy& AD;
Ted Kremenek0064ff42007-09-28 20:38:59 +000064 LiveVariables::ValTy LiveState;
Ted Kremenekb56a9902007-09-06 00:17:54 +000065public:
Ted Kremenekfb4750b2007-10-01 20:33:52 +000066 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekad8bce02007-09-25 04:31:27 +000067
Ted Kremenek0064ff42007-09-28 20:38:59 +000068 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek9d0acca2007-11-20 03:01:58 +000069 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek1147e362007-09-12 19:10:52 +000070
Ted Kremenekb56a9902007-09-06 00:17:54 +000071 void VisitDeclRefExpr(DeclRefExpr* DR);
72 void VisitBinaryOperator(BinaryOperator* B);
73 void VisitAssign(BinaryOperator* B);
Ted Kremenek3f8ed262007-09-06 21:26:58 +000074 void VisitDeclStmt(DeclStmt* DS);
75 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremenekfb4750b2007-10-01 20:33:52 +000076 void Visit(Stmt *S);
Ted Kremenekb56a9902007-09-06 00:17:54 +000077};
Ted Kremenekfb4750b2007-10-01 20:33:52 +000078
Ted Kremenekfb4750b2007-10-01 20:33:52 +000079void TransferFuncs::Visit(Stmt *S) {
80 if (AD.Observer)
81 AD.Observer->ObserveStmt(S,AD,LiveState);
Ted Kremenek0064ff42007-09-28 20:38:59 +000082
Ted Kremenek85be7cf2008-01-17 20:48:37 +000083 if (S == getCurrentBlkStmt()) {
Ted Kremenek0c046062008-03-05 19:26:46 +000084 if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
Ted Kremenek05ecfdd2008-01-18 00:40:21 +000085 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenek85be7cf2008-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 Kremenekfb4750b2007-10-01 20:33:52 +000092}
Ted Kremenekb56a9902007-09-06 00:17:54 +000093
Ted Kremenekad8bce02007-09-25 04:31:27 +000094void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
95 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenek0064ff42007-09-28 20:38:59 +000096 LiveState(V,AD) = Alive;
Ted Kremenekb56a9902007-09-06 00:17:54 +000097}
Ted Kremenekb56a9902007-09-06 00:17:54 +000098
Ted Kremenekad8bce02007-09-25 04:31:27 +000099void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek1147e362007-09-12 19:10:52 +0000100 if (B->isAssignmentOp()) VisitAssign(B);
101 else VisitStmt(B);
Ted Kremenekb56a9902007-09-06 00:17:54 +0000102}
103
Ted Kremenekad8bce02007-09-25 04:31:27 +0000104void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenekf1dae232008-01-17 17:50:49 +0000105 Expr *E = U->getSubExpr();
Ted Kremenek14851c32007-09-28 21:29:33 +0000106
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000107 switch (U->getOpcode()) {
Ted Kremenek78dcda62007-12-13 04:47:15 +0000108 case UnaryOperator::SizeOf: return;
Ted Kremenekad8bce02007-09-25 04:31:27 +0000109 case UnaryOperator::PostInc:
110 case UnaryOperator::PostDec:
111 case UnaryOperator::PreInc:
112 case UnaryOperator::PreDec:
Ted Kremenekad8bce02007-09-25 04:31:27 +0000113 // Walk through the subexpressions, blasting through ParenExprs
114 // until we either find a DeclRefExpr or some non-DeclRefExpr
115 // expression.
Ted Kremenek20c91422008-02-22 00:34:10 +0000116 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
Ted Kremenekcd76f952008-04-15 04:08:54 +0000117 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
118 // Treat the --/++ operator as a kill.
Ted Kremenek20c91422008-02-22 00:34:10 +0000119 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
Ted Kremenekcd76f952008-04-15 04:08:54 +0000120 LiveState(VD, AD) = Alive;
Ted Kremenek20c91422008-02-22 00:34:10 +0000121 return VisitDeclRefExpr(DR);
122 }
Ted Kremenek14851c32007-09-28 21:29:33 +0000123
124 // Fall-through.
Ted Kremenekad8bce02007-09-25 04:31:27 +0000125
126 default:
Ted Kremenekf1dae232008-01-17 17:50:49 +0000127 return Visit(E);
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000128 }
129}
Ted Kremenek14851c32007-09-28 21:29:33 +0000130
Ted Kremenekad8bce02007-09-25 04:31:27 +0000131void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenekf1dae232008-01-17 17:50:49 +0000132 Expr* LHS = B->getLHS();
Ted Kremenekad8bce02007-09-25 04:31:27 +0000133
Ted Kremenek14851c32007-09-28 21:29:33 +0000134 // Assigning to a variable?
Ted Kremenekf1dae232008-01-17 17:50:49 +0000135 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenekf4212bd2008-04-15 03:47:30 +0000136 LiveState(DR->getDecl(), AD) = Dead;
Ted Kremenek0064ff42007-09-28 20:38:59 +0000137 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
138
Ted Kremenekad8bce02007-09-25 04:31:27 +0000139 // Handle things like +=, etc., which also generate "uses"
140 // of a variable. Do this just by visiting the subexpression.
Ted Kremenek14851c32007-09-28 21:29:33 +0000141 if (B->getOpcode() != BinaryOperator::Assign)
142 VisitDeclRefExpr(DR);
Ted Kremenekb56a9902007-09-06 00:17:54 +0000143 }
Ted Kremenekad8bce02007-09-25 04:31:27 +0000144 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000145 Visit(LHS);
Ted Kremenekb56a9902007-09-06 00:17:54 +0000146
Ted Kremenekad8bce02007-09-25 04:31:27 +0000147 Visit(B->getRHS());
Ted Kremenekb56a9902007-09-06 00:17:54 +0000148}
149
Ted Kremenekad8bce02007-09-25 04:31:27 +0000150void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
151 // Declarations effectively "kill" a variable since they cannot
152 // possibly be live before they are declared.
Ted Kremenek7845b262008-02-25 22:28:54 +0000153 for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator())
154 if (VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremenekf4212bd2008-04-15 03:47:30 +0000155 LiveState(D, AD) = Dead;
Ted Kremenek7845b262008-02-25 22:28:54 +0000156
Ted Kremeneka56c08a2008-02-07 02:38:55 +0000157 if (Expr* Init = VD->getInit())
158 Visit(Init);
Ted Kremenek7845b262008-02-25 22:28:54 +0000159 }
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000160}
Ted Kremenek0064ff42007-09-28 20:38:59 +0000161
Ted Kremenekb56a9902007-09-06 00:17:54 +0000162} // end anonymous namespace
163
Ted Kremenek0064ff42007-09-28 20:38:59 +0000164//===----------------------------------------------------------------------===//
165// Merge operator: if something is live on any successor block, it is live
166// in the current block (a set union).
167//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +0000168
Ted Kremenekad8bce02007-09-25 04:31:27 +0000169namespace {
Ted Kremenekb7151c72008-03-20 21:46:49 +0000170
171struct Merge {
172 typedef ExprDeclBitVector_Types::ValTy ValTy;
173
174 void operator()(ValTy& Dst, const ValTy& Src) {
175 Dst.OrDeclBits(Src);
176 Dst.AndExprBits(Src);
177 }
178};
179
180typedef DataflowSolver<LiveVariables, TransferFuncs, Merge> Solver;
Ted Kremenek0064ff42007-09-28 20:38:59 +0000181} // end anonymous namespace
182
183//===----------------------------------------------------------------------===//
184// External interface to run Liveness analysis.
185//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +0000186
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +0000187void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000188 Solver S(*this);
189 S.runOnCFG(cfg);
190}
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000191
Ted Kremenekad8bce02007-09-25 04:31:27 +0000192void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenekb4b65e62008-01-17 18:25:22 +0000193 LiveVariables::ObserverTy* Obs,
194 bool recordStmtValues) {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000195 Solver S(*this);
196 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenekb4b65e62008-01-17 18:25:22 +0000197 getAnalysisData().Observer = Obs;
198 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekad8bce02007-09-25 04:31:27 +0000199 getAnalysisData().Observer = OldObserver;
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000200}
201
202//===----------------------------------------------------------------------===//
203// liveness queries
204//
205
Ted Kremenekbd9cc5c2007-09-10 17:36:42 +0000206bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenek1fdd0a42008-03-13 16:55:07 +0000207 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
208 return i.isValid() ? getBlockData(B).getBit(i) : false;
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000209}
210
Ted Kremenekad8bce02007-09-25 04:31:27 +0000211bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenek1fdd0a42008-03-13 16:55:07 +0000212 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
213 return i.isValid() ? Live.getBit(i) : false;
Ted Kremenek6dc7b112007-09-06 23:00:42 +0000214}
215
Ted Kremenek85be7cf2008-01-17 20:48:37 +0000216bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
217 return getStmtData(Loc)(StmtVal,getAnalysisData());
218}
219
Ted Kremenek05ecfdd2008-01-18 00:40:21 +0000220bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
221 return getStmtData(Loc)(D,getAnalysisData());
222}
223
Ted Kremenek6dc7b112007-09-06 23:00:42 +0000224//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +0000225// printing liveness state for debugging
226//
227
Ted Kremenekad8bce02007-09-25 04:31:27 +0000228void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
229 const AnalysisDataTy& AD = getAnalysisData();
230
Ted Kremenek0064ff42007-09-28 20:38:59 +0000231 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
232 E = AD.end_decl(); I!=E; ++I)
233 if (V.getDeclBit(I->second)) {
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000234 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekad8bce02007-09-25 04:31:27 +0000235
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000236 fprintf(stderr, " %s <%s:%u:%u>\n",
237 I->first->getIdentifier()->getName(),
238 SM.getSourceName(PhysLoc),
239 SM.getLineNumber(PhysLoc),
240 SM.getColumnNumber(PhysLoc));
Ted Kremenekb56a9902007-09-06 00:17:54 +0000241 }
Ted Kremenekb56a9902007-09-06 00:17:54 +0000242}
243
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000244void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000245 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
246 E = getBlockDataMap().end(); I!=E; ++I) {
247 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000248 I->first->getBlockID());
249
250 dumpLiveness(I->second,M);
251 }
Ted Kremenekbd9cc5c2007-09-10 17:36:42 +0000252
253 fprintf(stderr,"\n");
254}