blob: 59039fb14af2c6d8ff0e2d8d4f565c02bbd83687 [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
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 Kremenek8ce772b2007-10-01 20:33:52 +000054 RegisterDecls R(getAnalysisData());
Ted Kremenekd7a2f812007-09-25 04:31:27 +000055 cfg.VisitBlockStmts(R);
56}
Ted Kremenekaa04c512007-09-06 00:17:54 +000057
58//===----------------------------------------------------------------------===//
Ted Kremenekd7a2f812007-09-25 04:31:27 +000059// Transfer functions.
60//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +000061
62namespace {
Ted Kremenekeebf1312007-09-28 20:38:59 +000063
64static const bool Alive = true;
65static const bool Dead = false;
66
Ted Kremenekec818352008-01-08 18:19:08 +000067class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekd7a2f812007-09-25 04:31:27 +000068 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekeebf1312007-09-28 20:38:59 +000069 LiveVariables::ValTy LiveState;
Ted Kremenekaa04c512007-09-06 00:17:54 +000070public:
Ted Kremenek8ce772b2007-10-01 20:33:52 +000071 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekd7a2f812007-09-25 04:31:27 +000072
Ted Kremenekeebf1312007-09-28 20:38:59 +000073 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek705386b2007-11-20 03:01:58 +000074 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek00b63272007-09-12 19:10:52 +000075
Ted Kremenekaa04c512007-09-06 00:17:54 +000076 void VisitDeclRefExpr(DeclRefExpr* DR);
77 void VisitBinaryOperator(BinaryOperator* B);
78 void VisitAssign(BinaryOperator* B);
Ted Kremenek05334682007-09-06 21:26:58 +000079 void VisitDeclStmt(DeclStmt* DS);
80 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremenek8ce772b2007-10-01 20:33:52 +000081 void Visit(Stmt *S);
Ted Kremenekaa04c512007-09-06 00:17:54 +000082};
Ted Kremenek8ce772b2007-10-01 20:33:52 +000083
Ted Kremenek8ce772b2007-10-01 20:33:52 +000084void TransferFuncs::Visit(Stmt *S) {
85 if (AD.Observer)
86 AD.Observer->ObserveStmt(S,AD,LiveState);
Ted Kremenekeebf1312007-09-28 20:38:59 +000087
Ted Kremenekab6c5902008-01-17 20:48:37 +000088 if (S == getCurrentBlkStmt()) {
Ted Kremenekab6c5902008-01-17 20:48:37 +000089 if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
Ted Kremenek1796e132008-01-18 00:40:21 +000090 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenekab6c5902008-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 Kremenek8ce772b2007-10-01 20:33:52 +000097}
Ted Kremenekaa04c512007-09-06 00:17:54 +000098
Ted Kremenekd7a2f812007-09-25 04:31:27 +000099void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
100 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekeebf1312007-09-28 20:38:59 +0000101 LiveState(V,AD) = Alive;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000102}
Ted Kremenekaa04c512007-09-06 00:17:54 +0000103
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000104void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek00b63272007-09-12 19:10:52 +0000105 if (B->isAssignmentOp()) VisitAssign(B);
106 else VisitStmt(B);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000107}
108
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000109void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek133befa2008-01-17 17:50:49 +0000110 Expr *E = U->getSubExpr();
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000111
Ted Kremenek05334682007-09-06 21:26:58 +0000112 switch (U->getOpcode()) {
Ted Kremeneke33d1002007-12-13 04:47:15 +0000113 case UnaryOperator::SizeOf: return;
Ted Kremenekd7a2f812007-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 Kremenek2c3041c2008-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 Kremenek83a6ba12007-09-28 21:29:33 +0000129
130 // Fall-through.
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000131
132 default:
Ted Kremenek133befa2008-01-17 17:50:49 +0000133 return Visit(E);
Ted Kremenek05334682007-09-06 21:26:58 +0000134 }
135}
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000136
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000137void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek133befa2008-01-17 17:50:49 +0000138 Expr* LHS = B->getLHS();
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000139
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000140 // Assigning to a variable?
Ted Kremenek133befa2008-01-17 17:50:49 +0000141 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000142 LiveState(DR->getDecl(),AD) = Dead;
143 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
144
Ted Kremenekd7a2f812007-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 Kremenek83a6ba12007-09-28 21:29:33 +0000147 if (B->getOpcode() != BinaryOperator::Assign)
148 VisitDeclRefExpr(DR);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000149 }
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000150 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek05334682007-09-06 21:26:58 +0000151 Visit(LHS);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000152
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000153 Visit(B->getRHS());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000154}
155
Ted Kremenekd7a2f812007-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 Kremenek0c5fe982008-02-07 02:38:55 +0000159 for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator()) {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000160 LiveState(D,AD) = Dead;
Ted Kremenek0c5fe982008-02-07 02:38:55 +0000161
162 if (VarDecl* VD = dyn_cast<VarDecl>(D))
163 if (Expr* Init = VD->getInit())
164 Visit(Init);
165 }
Ted Kremenek05334682007-09-06 21:26:58 +0000166}
Ted Kremenekeebf1312007-09-28 20:38:59 +0000167
Ted Kremenekaa04c512007-09-06 00:17:54 +0000168} // end anonymous namespace
169
Ted Kremenekeebf1312007-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 Kremenekaa04c512007-09-06 00:17:54 +0000174
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000175namespace {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000176typedef DeclBitVector_Types::Union Merge;
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000177typedef DataflowSolver<LiveVariables,TransferFuncs,Merge> Solver;
Ted Kremenekeebf1312007-09-28 20:38:59 +0000178} // end anonymous namespace
179
180//===----------------------------------------------------------------------===//
181// External interface to run Liveness analysis.
182//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000183
Ted Kremenek5ee98a72008-01-11 00:40:29 +0000184void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000185 Solver S(*this);
186 S.runOnCFG(cfg);
187}
Ted Kremenek05334682007-09-06 21:26:58 +0000188
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000189void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenekab8ed952008-01-17 18:25:22 +0000190 LiveVariables::ObserverTy* Obs,
191 bool recordStmtValues) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000192 Solver S(*this);
193 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenekab8ed952008-01-17 18:25:22 +0000194 getAnalysisData().Observer = Obs;
195 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000196 getAnalysisData().Observer = OldObserver;
Ted Kremenek05334682007-09-06 21:26:58 +0000197}
198
199//===----------------------------------------------------------------------===//
200// liveness queries
201//
202
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000203bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000204 return getBlockData(B)(D,getAnalysisData());
Ted Kremenek05334682007-09-06 21:26:58 +0000205}
206
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000207bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000208 return Live(D,getAnalysisData());
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000209}
210
Ted Kremenekab6c5902008-01-17 20:48:37 +0000211bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
212 return getStmtData(Loc)(StmtVal,getAnalysisData());
213}
214
Ted Kremenek1796e132008-01-18 00:40:21 +0000215bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
216 return getStmtData(Loc)(D,getAnalysisData());
217}
218
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000219//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000220// printing liveness state for debugging
221//
222
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000223void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
224 const AnalysisDataTy& AD = getAnalysisData();
225
Ted Kremenekeebf1312007-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 Kremenek05334682007-09-06 21:26:58 +0000229 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000230
Ted Kremenek05334682007-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 Kremenekaa04c512007-09-06 00:17:54 +0000236 }
Ted Kremenekaa04c512007-09-06 00:17:54 +0000237}
238
Ted Kremenek05334682007-09-06 21:26:58 +0000239void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekd7a2f812007-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 Kremenek05334682007-09-06 21:26:58 +0000243 I->first->getBlockID());
244
245 dumpLiveness(I->second,M);
246 }
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000247
248 fprintf(stderr,"\n");
249}