blob: 52f7f4d19bf98abe8e8ceb34583944b24640e491 [file] [log] [blame]
Ted Kremenek11de5cb2007-09-20 21:42:55 +00001//=- LiveVariables.cpp - Live Variable Analysis for Source CFGs -*- C++ --*-==//
Ted Kremeneke4e63342007-09-06 00:17:54 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Kremenekfdd225e2007-09-25 04:31:27 +00007// details.
Ted Kremeneke4e63342007-09-06 00:17:54 +00008//
9//===----------------------------------------------------------------------===//
10//
11// This file implements Live Variables analysis for source-level CFGs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekcf6e41b2007-12-21 21:42:19 +000015#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek27b07c52007-09-06 21:26:58 +000016#include "clang/Basic/SourceManager.h"
Ted Kremeneke4e63342007-09-06 00:17:54 +000017#include "clang/AST/Expr.h"
18#include "clang/AST/CFG.h"
Ted Kremenekfdd225e2007-09-25 04:31:27 +000019#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenek1de632b2007-09-25 21:00:24 +000020#include "clang/Analysis/FlowSensitive/DataflowSolver.h"
Ted Kremeneke4e63342007-09-06 00:17:54 +000021#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenekc2b51d82008-01-08 18:19:08 +000022#include "llvm/Support/Compiler.h"
Ted Kremeneke4e63342007-09-06 00:17:54 +000023
Ted Kremenek27b07c52007-09-06 21:26:58 +000024#include <string.h>
25#include <stdio.h>
Ted Kremeneke4e63342007-09-06 00:17:54 +000026
27using namespace clang;
28
29//===----------------------------------------------------------------------===//
Ted Kremenekfdd225e2007-09-25 04:31:27 +000030// Dataflow initialization logic.
31//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +000032
33namespace {
Ted Kremenekc2b51d82008-01-08 18:19:08 +000034class VISIBILITY_HIDDEN RegisterDecls
35 : public CFGRecStmtDeclVisitor<RegisterDecls> {
36
Ted Kremenekfdd225e2007-09-25 04:31:27 +000037 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekf63aa452007-09-28 20:38:59 +000038public:
Ted Kremenek11e72182007-10-01 20:33:52 +000039 RegisterDecls(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekf63aa452007-09-28 20:38:59 +000040 void VisitVarDecl(VarDecl* VD) { AD.Register(VD); }
Ted Kremenek9f9141c2007-11-20 03:01:58 +000041 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek11e72182007-10-01 20:33:52 +000042};
Ted Kremeneke4e63342007-09-06 00:17:54 +000043} // end anonymous namespace
44
Ted Kremenekbffaa832008-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 Kremenek11e72182007-10-01 20:33:52 +000054 RegisterDecls R(getAnalysisData());
Ted Kremenekfdd225e2007-09-25 04:31:27 +000055 cfg.VisitBlockStmts(R);
56}
Ted Kremeneke4e63342007-09-06 00:17:54 +000057
58//===----------------------------------------------------------------------===//
Ted Kremenekfdd225e2007-09-25 04:31:27 +000059// Transfer functions.
60//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +000061
62namespace {
Ted Kremenekf63aa452007-09-28 20:38:59 +000063
64static const bool Alive = true;
65static const bool Dead = false;
66
Ted Kremenekc2b51d82008-01-08 18:19:08 +000067class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekfdd225e2007-09-25 04:31:27 +000068 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekf63aa452007-09-28 20:38:59 +000069 LiveVariables::ValTy LiveState;
Ted Kremeneke4e63342007-09-06 00:17:54 +000070public:
Ted Kremenek11e72182007-10-01 20:33:52 +000071 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekfdd225e2007-09-25 04:31:27 +000072
Ted Kremenekf63aa452007-09-28 20:38:59 +000073 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek9f9141c2007-11-20 03:01:58 +000074 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenekf1758052007-09-12 19:10:52 +000075
Ted Kremeneke4e63342007-09-06 00:17:54 +000076 void VisitDeclRefExpr(DeclRefExpr* DR);
77 void VisitBinaryOperator(BinaryOperator* B);
78 void VisitAssign(BinaryOperator* B);
Ted Kremenek27b07c52007-09-06 21:26:58 +000079 void VisitDeclStmt(DeclStmt* DS);
80 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremenek11e72182007-10-01 20:33:52 +000081 void Visit(Stmt *S);
Ted Kremeneke4e63342007-09-06 00:17:54 +000082};
Ted Kremenek11e72182007-10-01 20:33:52 +000083
Ted Kremenek11e72182007-10-01 20:33:52 +000084void TransferFuncs::Visit(Stmt *S) {
85 if (AD.Observer)
86 AD.Observer->ObserveStmt(S,AD,LiveState);
Ted Kremenekf63aa452007-09-28 20:38:59 +000087
Ted Kremenek86946742008-01-17 20:48:37 +000088 if (S == getCurrentBlkStmt()) {
Ted Kremenek86946742008-01-17 20:48:37 +000089 if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
Ted Kremenek2a9da9c2008-01-18 00:40:21 +000090 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenek86946742008-01-17 20:48:37 +000091 }
92 else if (!getCFG().isBlkExpr(S))
93 StmtVisitor<TransferFuncs,void>::Visit(S);
94 else
95 // For block-level expressions, mark that they are live.
96 LiveState(S,AD) = Alive;
Ted Kremenek11e72182007-10-01 20:33:52 +000097}
Ted Kremeneke4e63342007-09-06 00:17:54 +000098
Ted Kremenekfdd225e2007-09-25 04:31:27 +000099void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
100 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekf63aa452007-09-28 20:38:59 +0000101 LiveState(V,AD) = Alive;
Ted Kremeneke4e63342007-09-06 00:17:54 +0000102}
Ted Kremeneke4e63342007-09-06 00:17:54 +0000103
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000104void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenekf1758052007-09-12 19:10:52 +0000105 if (B->isAssignmentOp()) VisitAssign(B);
106 else VisitStmt(B);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000107}
108
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000109void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000110 Expr *E = U->getSubExpr();
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000111
Ted Kremenek27b07c52007-09-06 21:26:58 +0000112 switch (U->getOpcode()) {
Ted Kremenek8d9ebae2007-12-13 04:47:15 +0000113 case UnaryOperator::SizeOf: return;
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000114 case UnaryOperator::PostInc:
115 case UnaryOperator::PostDec:
116 case UnaryOperator::PreInc:
117 case UnaryOperator::PreDec:
118 case UnaryOperator::AddrOf:
119 // Walk through the subexpressions, blasting through ParenExprs
120 // until we either find a DeclRefExpr or some non-DeclRefExpr
121 // expression.
Ted Kremenek56206312008-02-22 00:34:10 +0000122 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
123 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
124 // Treat the --/++/& operator as a kill.
125 LiveState(VD, AD) = Dead;
126 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
127 return VisitDeclRefExpr(DR);
128 }
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000129
130 // Fall-through.
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000131
132 default:
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000133 return Visit(E);
Ted Kremenek27b07c52007-09-06 21:26:58 +0000134 }
135}
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000136
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000137void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000138 Expr* LHS = B->getLHS();
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000139
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000140 // Assigning to a variable?
Ted Kremenek5e2b6092008-01-17 17:50:49 +0000141 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000142 LiveState(DR->getDecl(),AD) = Dead;
143 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
144
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000145 // Handle things like +=, etc., which also generate "uses"
146 // of a variable. Do this just by visiting the subexpression.
Ted Kremenekbcb07d52007-09-28 21:29:33 +0000147 if (B->getOpcode() != BinaryOperator::Assign)
148 VisitDeclRefExpr(DR);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000149 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000150 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek27b07c52007-09-06 21:26:58 +0000151 Visit(LHS);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000152
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000153 Visit(B->getRHS());
Ted Kremeneke4e63342007-09-06 00:17:54 +0000154}
155
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000156void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
157 // Declarations effectively "kill" a variable since they cannot
158 // possibly be live before they are declared.
Ted Kremenekdcc48102008-02-25 22:28:54 +0000159 for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator())
160 if (VarDecl* VD = dyn_cast<VarDecl>(D)) {
161 LiveState(D,AD) = Dead;
162
Ted Kremenek2bca5e42008-02-07 02:38:55 +0000163 if (Expr* Init = VD->getInit())
164 Visit(Init);
Ted Kremenekdcc48102008-02-25 22:28:54 +0000165 }
Ted Kremenek27b07c52007-09-06 21:26:58 +0000166}
Ted Kremenekf63aa452007-09-28 20:38:59 +0000167
Ted Kremeneke4e63342007-09-06 00:17:54 +0000168} // end anonymous namespace
169
Ted Kremenekf63aa452007-09-28 20:38:59 +0000170//===----------------------------------------------------------------------===//
171// Merge operator: if something is live on any successor block, it is live
172// in the current block (a set union).
173//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000174
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000175namespace {
Ted Kremenekd156d532008-02-22 23:17:20 +0000176typedef ExprDeclBitVector_Types::Union Merge;
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000177typedef DataflowSolver<LiveVariables,TransferFuncs,Merge> Solver;
Ted Kremenekf63aa452007-09-28 20:38:59 +0000178} // end anonymous namespace
179
180//===----------------------------------------------------------------------===//
181// External interface to run Liveness analysis.
182//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000183
Ted Kremenek83c01da2008-01-11 00:40:29 +0000184void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000185 Solver S(*this);
186 S.runOnCFG(cfg);
187}
Ted Kremenek27b07c52007-09-06 21:26:58 +0000188
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000189void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenek79649df2008-01-17 18:25:22 +0000190 LiveVariables::ObserverTy* Obs,
191 bool recordStmtValues) {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000192 Solver S(*this);
193 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenek79649df2008-01-17 18:25:22 +0000194 getAnalysisData().Observer = Obs;
195 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000196 getAnalysisData().Observer = OldObserver;
Ted Kremenek27b07c52007-09-06 21:26:58 +0000197}
198
199//===----------------------------------------------------------------------===//
200// liveness queries
201//
202
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000203bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000204 return getBlockData(B)(D,getAnalysisData());
Ted Kremenek27b07c52007-09-06 21:26:58 +0000205}
206
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000207bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenekf63aa452007-09-28 20:38:59 +0000208 return Live(D,getAnalysisData());
Ted Kremenek055c2752007-09-06 23:00:42 +0000209}
210
Ted Kremenek86946742008-01-17 20:48:37 +0000211bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
212 return getStmtData(Loc)(StmtVal,getAnalysisData());
213}
214
Ted Kremenek2a9da9c2008-01-18 00:40:21 +0000215bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
216 return getStmtData(Loc)(D,getAnalysisData());
217}
218
Ted Kremenek055c2752007-09-06 23:00:42 +0000219//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000220// printing liveness state for debugging
221//
222
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000223void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
224 const AnalysisDataTy& AD = getAnalysisData();
225
Ted Kremenekf63aa452007-09-28 20:38:59 +0000226 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
227 E = AD.end_decl(); I!=E; ++I)
228 if (V.getDeclBit(I->second)) {
Ted Kremenek27b07c52007-09-06 21:26:58 +0000229 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000230
Ted Kremenek27b07c52007-09-06 21:26:58 +0000231 fprintf(stderr, " %s <%s:%u:%u>\n",
232 I->first->getIdentifier()->getName(),
233 SM.getSourceName(PhysLoc),
234 SM.getLineNumber(PhysLoc),
235 SM.getColumnNumber(PhysLoc));
Ted Kremeneke4e63342007-09-06 00:17:54 +0000236 }
Ted Kremeneke4e63342007-09-06 00:17:54 +0000237}
238
Ted Kremenek27b07c52007-09-06 21:26:58 +0000239void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000240 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
241 E = getBlockDataMap().end(); I!=E; ++I) {
242 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek27b07c52007-09-06 21:26:58 +0000243 I->first->getBlockID());
244
245 dumpLiveness(I->second,M);
246 }
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000247
248 fprintf(stderr,"\n");
249}