blob: c14b463be164571220ac7f2aea73e3efb1dd5563 [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 Kremenek133befa2008-01-17 17:50:49 +0000122 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000123 // Treat the --/++/& operator as a kill.
124 LiveState(DR->getDecl(),AD) = Dead;
125 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
126 return VisitDeclRefExpr(DR);
Ted Kremenekeebf1312007-09-28 20:38:59 +0000127 }
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000128
129 // Fall-through.
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000130
131 default:
Ted Kremenek133befa2008-01-17 17:50:49 +0000132 return Visit(E);
Ted Kremenek05334682007-09-06 21:26:58 +0000133 }
134}
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000135
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000136void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek133befa2008-01-17 17:50:49 +0000137 Expr* LHS = B->getLHS();
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000138
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000139 // Assigning to a variable?
Ted Kremenek133befa2008-01-17 17:50:49 +0000140 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000141 LiveState(DR->getDecl(),AD) = Dead;
142 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
143
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000144 // Handle things like +=, etc., which also generate "uses"
145 // of a variable. Do this just by visiting the subexpression.
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000146 if (B->getOpcode() != BinaryOperator::Assign)
147 VisitDeclRefExpr(DR);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000148 }
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000149 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek05334682007-09-06 21:26:58 +0000150 Visit(LHS);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000151
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000152 Visit(B->getRHS());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000153}
154
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000155void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
156 // Declarations effectively "kill" a variable since they cannot
157 // possibly be live before they are declared.
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000158 for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator())
Ted Kremenekeebf1312007-09-28 20:38:59 +0000159 LiveState(D,AD) = Dead;
Ted Kremenek05334682007-09-06 21:26:58 +0000160}
Ted Kremenekeebf1312007-09-28 20:38:59 +0000161
Ted Kremenekaa04c512007-09-06 00:17:54 +0000162} // end anonymous namespace
163
Ted Kremenekeebf1312007-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 Kremenekaa04c512007-09-06 00:17:54 +0000168
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000169namespace {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000170typedef DeclBitVector_Types::Union Merge;
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000171typedef DataflowSolver<LiveVariables,TransferFuncs,Merge> Solver;
Ted Kremenekeebf1312007-09-28 20:38:59 +0000172} // end anonymous namespace
173
174//===----------------------------------------------------------------------===//
175// External interface to run Liveness analysis.
176//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000177
Ted Kremenek5ee98a72008-01-11 00:40:29 +0000178void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000179 Solver S(*this);
180 S.runOnCFG(cfg);
181}
Ted Kremenek05334682007-09-06 21:26:58 +0000182
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000183void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenekab8ed952008-01-17 18:25:22 +0000184 LiveVariables::ObserverTy* Obs,
185 bool recordStmtValues) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000186 Solver S(*this);
187 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenekab8ed952008-01-17 18:25:22 +0000188 getAnalysisData().Observer = Obs;
189 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000190 getAnalysisData().Observer = OldObserver;
Ted Kremenek05334682007-09-06 21:26:58 +0000191}
192
193//===----------------------------------------------------------------------===//
194// liveness queries
195//
196
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000197bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000198 return getBlockData(B)(D,getAnalysisData());
Ted Kremenek05334682007-09-06 21:26:58 +0000199}
200
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000201bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000202 return Live(D,getAnalysisData());
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000203}
204
Ted Kremenekab6c5902008-01-17 20:48:37 +0000205bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
206 return getStmtData(Loc)(StmtVal,getAnalysisData());
207}
208
Ted Kremenek1796e132008-01-18 00:40:21 +0000209bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
210 return getStmtData(Loc)(D,getAnalysisData());
211}
212
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000213//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000214// printing liveness state for debugging
215//
216
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000217void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
218 const AnalysisDataTy& AD = getAnalysisData();
219
Ted Kremenekeebf1312007-09-28 20:38:59 +0000220 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
221 E = AD.end_decl(); I!=E; ++I)
222 if (V.getDeclBit(I->second)) {
Ted Kremenek05334682007-09-06 21:26:58 +0000223 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000224
Ted Kremenek05334682007-09-06 21:26:58 +0000225 fprintf(stderr, " %s <%s:%u:%u>\n",
226 I->first->getIdentifier()->getName(),
227 SM.getSourceName(PhysLoc),
228 SM.getLineNumber(PhysLoc),
229 SM.getColumnNumber(PhysLoc));
Ted Kremenekaa04c512007-09-06 00:17:54 +0000230 }
Ted Kremenekaa04c512007-09-06 00:17:54 +0000231}
232
Ted Kremenek05334682007-09-06 21:26:58 +0000233void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000234 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
235 E = getBlockDataMap().end(); I!=E; ++I) {
236 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek05334682007-09-06 21:26:58 +0000237 I->first->getBlockID());
238
239 dumpLiveness(I->second,M);
240 }
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000241
242 fprintf(stderr,"\n");
243}