Ted Kremenek | 26e4746 | 2007-09-20 21:42:55 +0000 | [diff] [blame] | 1 | //=- LiveVariables.cpp - Live Variable Analysis for Source CFGs -*- C++ --*-==// |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Ted Kremenek and is distributed under |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 6 | // the University of Illinois Open Source License. See LICENSE.TXT for |
| 7 | // details. |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | // |
| 11 | // This file implements Live Variables analysis for source-level CFGs. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Analysis/LiveVariables.h" |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
| 18 | #include "clang/AST/CFG.h" |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h" |
Ted Kremenek | 10d8046 | 2007-09-25 21:00:24 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/FlowSensitive/DataflowSolver.h" |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 21 | #include "clang/Lex/IdentifierTable.h" |
| 22 | #include "llvm/ADT/SmallPtrSet.h" |
| 23 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 24 | #include <string.h> |
| 25 | #include <stdio.h> |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace clang; |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 30 | // Dataflow initialization logic. |
| 31 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 32 | |
| 33 | namespace { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 34 | class RegisterDeclsExprs : public CFGRecStmtDeclVisitor<RegisterDeclsExprs> { |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 35 | LiveVariables::AnalysisDataTy& AD; |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 36 | public: |
| 37 | RegisterDeclsExprs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {} |
| 38 | |
| 39 | void VisitVarDecl(VarDecl* VD) { AD.Register(VD); } |
| 40 | void BlockStmt_VisitExpr(Expr* E) { AD.Register(E); } |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 41 | }; |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 42 | } // end anonymous namespace |
| 43 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 44 | void LiveVariables::InitializeValues(const CFG& cfg) { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 45 | RegisterDeclsExprs R(getAnalysisData()); |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 46 | cfg.VisitBlockStmts(R); |
| 47 | } |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 48 | |
| 49 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 50 | // Transfer functions. |
| 51 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 52 | |
| 53 | namespace { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 54 | |
| 55 | static const bool Alive = true; |
| 56 | static const bool Dead = false; |
| 57 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 58 | class TransferFuncs : public CFGStmtVisitor<TransferFuncs> { |
| 59 | LiveVariables::AnalysisDataTy& AD; |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 60 | LiveVariables::ValTy LiveState; |
| 61 | bool ExprLiveness; |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 62 | public: |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 63 | TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad), |
| 64 | ExprLiveness(Dead) {} |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 66 | LiveVariables::ValTy& getVal() { return LiveState; } |
Ted Kremenek | 00b6327 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 68 | void VisitDeclRefExpr(DeclRefExpr* DR); |
| 69 | void VisitBinaryOperator(BinaryOperator* B); |
| 70 | void VisitAssign(BinaryOperator* B); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 71 | void VisitDeclStmt(DeclStmt* DS); |
| 72 | void VisitUnaryOperator(UnaryOperator* U); |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 73 | void VisitStmt(Stmt* S); |
| 74 | void BlockStmt_VisitExpr(Expr *E); |
| 75 | |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame^] | 76 | DeclRefExpr* FindDeclRef(Stmt *S); |
| 77 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 78 | void Visit(Stmt *S) { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 79 | if (AD.Observer) AD.Observer->ObserveStmt(S,AD,LiveState); |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 80 | static_cast<CFGStmtVisitor<TransferFuncs>*>(this)->Visit(S); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 81 | } |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 82 | }; |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 83 | |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 84 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 85 | void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) { |
| 86 | if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl())) |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 87 | LiveState(V,AD) = Alive; |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 88 | } |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 90 | void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) { |
Ted Kremenek | 00b6327 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 91 | if (B->isAssignmentOp()) VisitAssign(B); |
| 92 | else VisitStmt(B); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 95 | void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) { |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame^] | 96 | Stmt *S = U->getSubExpr(); |
| 97 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 98 | switch (U->getOpcode()) { |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 99 | case UnaryOperator::PostInc: |
| 100 | case UnaryOperator::PostDec: |
| 101 | case UnaryOperator::PreInc: |
| 102 | case UnaryOperator::PreDec: |
| 103 | case UnaryOperator::AddrOf: |
| 104 | // Walk through the subexpressions, blasting through ParenExprs |
| 105 | // until we either find a DeclRefExpr or some non-DeclRefExpr |
| 106 | // expression. |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame^] | 107 | if (DeclRefExpr* DR = FindDeclRef(S)) { |
| 108 | // Treat the --/++/& operator as a kill. |
| 109 | LiveState(DR->getDecl(),AD) = Dead; |
| 110 | if (AD.Observer) { AD.Observer->ObserverKill(DR); } |
| 111 | return VisitDeclRefExpr(DR); |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 112 | } |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame^] | 113 | |
| 114 | // Fall-through. |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 115 | |
| 116 | default: |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame^] | 117 | return Visit(S); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame^] | 121 | DeclRefExpr* TransferFuncs::FindDeclRef(Stmt *S) { |
| 122 | for (;;) |
| 123 | if (ParenExpr* P = dyn_cast<ParenExpr>(S)) { |
| 124 | S = P->getSubExpr(); continue; |
| 125 | } |
| 126 | else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) |
| 127 | return DR; |
| 128 | else |
| 129 | return NULL; |
| 130 | } |
| 131 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 132 | void TransferFuncs::VisitAssign(BinaryOperator* B) { |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 133 | Stmt* LHS = B->getLHS(); |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 134 | |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame^] | 135 | // Assigning to a variable? |
| 136 | if (DeclRefExpr* DR = FindDeclRef(LHS)) { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 137 | LiveState(DR->getDecl(),AD) = Dead; |
| 138 | if (AD.Observer) { AD.Observer->ObserverKill(DR); } |
| 139 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 140 | // Handle things like +=, etc., which also generate "uses" |
| 141 | // of a variable. Do this just by visiting the subexpression. |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame^] | 142 | if (B->getOpcode() != BinaryOperator::Assign) |
| 143 | VisitDeclRefExpr(DR); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 144 | } |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 145 | else // Not assigning to a variable. Process LHS as usual. |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 146 | Visit(LHS); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 147 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 148 | Visit(B->getRHS()); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 151 | void TransferFuncs::VisitDeclStmt(DeclStmt* DS) { |
| 152 | // Declarations effectively "kill" a variable since they cannot |
| 153 | // possibly be live before they are declared. |
Steve Naroff | 2591e1b | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 154 | for (ScopedDecl* D = DS->getDecl(); D != NULL ; D = D->getNextDeclarator()) |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 155 | LiveState(D,AD) = Dead; |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 156 | } |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 157 | |
| 158 | void TransferFuncs::VisitStmt(Stmt* S) { |
| 159 | if (AD.isTracked(static_cast<Expr*>(S))) return; |
| 160 | else VisitChildren(S); |
| 161 | } |
| 162 | |
| 163 | void TransferFuncs::BlockStmt_VisitExpr(Expr* E) { |
| 164 | assert (AD.isTracked(E)); |
| 165 | static_cast<CFGStmtVisitor<TransferFuncs>*>(this)->Visit(E); |
| 166 | } |
| 167 | |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 168 | } // end anonymous namespace |
| 169 | |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 170 | //===----------------------------------------------------------------------===// |
| 171 | // Merge operator: if something is live on any successor block, it is live |
| 172 | // in the current block (a set union). |
| 173 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 174 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 175 | namespace { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 176 | typedef DeclBitVector_Types::Union Merge; |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 177 | typedef DataflowSolver<LiveVariables,TransferFuncs,Merge> Solver; |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 178 | } // end anonymous namespace |
| 179 | |
| 180 | //===----------------------------------------------------------------------===// |
| 181 | // External interface to run Liveness analysis. |
| 182 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 183 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 184 | void LiveVariables::runOnCFG(const CFG& cfg) { |
| 185 | Solver S(*this); |
| 186 | S.runOnCFG(cfg); |
| 187 | } |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 188 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 189 | void LiveVariables::runOnAllBlocks(const CFG& cfg, |
| 190 | LiveVariables::ObserverTy& Obs) { |
| 191 | Solver S(*this); |
| 192 | ObserverTy* OldObserver = getAnalysisData().Observer; |
| 193 | getAnalysisData().Observer = &Obs; |
| 194 | S.runOnAllBlocks(cfg); |
| 195 | getAnalysisData().Observer = OldObserver; |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | //===----------------------------------------------------------------------===// |
| 199 | // liveness queries |
| 200 | // |
| 201 | |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 202 | bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 203 | return getBlockData(B)(D,getAnalysisData()); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 206 | bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 207 | return Live(D,getAnalysisData()); |
Ted Kremenek | e805c4a | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Ted Kremenek | e805c4a | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 210 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 211 | // printing liveness state for debugging |
| 212 | // |
| 213 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 214 | void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const { |
| 215 | const AnalysisDataTy& AD = getAnalysisData(); |
| 216 | |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 217 | for (AnalysisDataTy::decl_iterator I = AD.begin_decl(), |
| 218 | E = AD.end_decl(); I!=E; ++I) |
| 219 | if (V.getDeclBit(I->second)) { |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 220 | SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation()); |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 221 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 222 | fprintf(stderr, " %s <%s:%u:%u>\n", |
| 223 | I->first->getIdentifier()->getName(), |
| 224 | SM.getSourceName(PhysLoc), |
| 225 | SM.getLineNumber(PhysLoc), |
| 226 | SM.getColumnNumber(PhysLoc)); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 227 | } |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 230 | void LiveVariables::dumpBlockLiveness(SourceManager& M) const { |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 231 | for (BlockDataMapTy::iterator I = getBlockDataMap().begin(), |
| 232 | E = getBlockDataMap().end(); I!=E; ++I) { |
| 233 | fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n", |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 234 | I->first->getBlockID()); |
| 235 | |
| 236 | dumpLiveness(I->second,M); |
| 237 | } |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 238 | |
| 239 | fprintf(stderr,"\n"); |
| 240 | } |