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 | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 76 | void Visit(Stmt *S) { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 77 | if (AD.Observer) AD.Observer->ObserveStmt(S,AD,LiveState); |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 78 | static_cast<CFGStmtVisitor<TransferFuncs>*>(this)->Visit(S); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 79 | } |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 80 | }; |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 81 | |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 82 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 83 | void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) { |
| 84 | if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl())) |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 85 | LiveState(V,AD) = Alive; |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 86 | } |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 88 | void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) { |
Ted Kremenek | 00b6327 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 89 | if (B->isAssignmentOp()) VisitAssign(B); |
| 90 | else VisitStmt(B); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 93 | void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) { |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 94 | switch (U->getOpcode()) { |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 95 | case UnaryOperator::PostInc: |
| 96 | case UnaryOperator::PostDec: |
| 97 | case UnaryOperator::PreInc: |
| 98 | case UnaryOperator::PreDec: |
| 99 | case UnaryOperator::AddrOf: |
| 100 | // Walk through the subexpressions, blasting through ParenExprs |
| 101 | // until we either find a DeclRefExpr or some non-DeclRefExpr |
| 102 | // expression. |
| 103 | for (Stmt* S = U->getSubExpr() ;;) { |
| 104 | if (ParenExpr* P = dyn_cast<ParenExpr>(S)) { S=P->getSubExpr(); continue;} |
| 105 | else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) { |
| 106 | // Treat the --/++/& operator as a kill. |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 107 | LiveState(DR->getDecl(),AD) = Dead; |
| 108 | if (AD.Observer) { AD.Observer->ObserverKill(DR); } |
| 109 | return VisitDeclRefExpr(DR); |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 110 | } |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 111 | else return Visit(S); |
| 112 | } |
| 113 | |
| 114 | assert (false && "Unreachable."); |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 115 | |
| 116 | default: |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 117 | return Visit(U->getSubExpr()); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 121 | void TransferFuncs::VisitAssign(BinaryOperator* B) { |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 122 | Stmt* LHS = B->getLHS(); |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 124 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) { // Assigning to a var? |
| 125 | LiveState(DR->getDecl(),AD) = Dead; |
| 126 | if (AD.Observer) { AD.Observer->ObserverKill(DR); } |
| 127 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 128 | // Handle things like +=, etc., which also generate "uses" |
| 129 | // of a variable. Do this just by visiting the subexpression. |
| 130 | if (B->getOpcode() != BinaryOperator::Assign) Visit(LHS); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 131 | } |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 132 | else // Not assigning to a variable. Process LHS as usual. |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 133 | Visit(LHS); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 134 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 135 | Visit(B->getRHS()); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 138 | void TransferFuncs::VisitDeclStmt(DeclStmt* DS) { |
| 139 | // Declarations effectively "kill" a variable since they cannot |
| 140 | // possibly be live before they are declared. |
Steve Naroff | 2591e1b | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 141 | for (ScopedDecl* D = DS->getDecl(); D != NULL ; D = D->getNextDeclarator()) |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 142 | LiveState(D,AD) = Dead; |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 143 | } |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 144 | |
| 145 | void TransferFuncs::VisitStmt(Stmt* S) { |
| 146 | if (AD.isTracked(static_cast<Expr*>(S))) return; |
| 147 | else VisitChildren(S); |
| 148 | } |
| 149 | |
| 150 | void TransferFuncs::BlockStmt_VisitExpr(Expr* E) { |
| 151 | assert (AD.isTracked(E)); |
| 152 | static_cast<CFGStmtVisitor<TransferFuncs>*>(this)->Visit(E); |
| 153 | } |
| 154 | |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 155 | } // end anonymous namespace |
| 156 | |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 157 | //===----------------------------------------------------------------------===// |
| 158 | // Merge operator: if something is live on any successor block, it is live |
| 159 | // in the current block (a set union). |
| 160 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 161 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 162 | namespace { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 163 | typedef DeclBitVector_Types::Union Merge; |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 164 | typedef DataflowSolver<LiveVariables,TransferFuncs,Merge> Solver; |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 165 | } // end anonymous namespace |
| 166 | |
| 167 | //===----------------------------------------------------------------------===// |
| 168 | // External interface to run Liveness analysis. |
| 169 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 170 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 171 | void LiveVariables::runOnCFG(const CFG& cfg) { |
| 172 | Solver S(*this); |
| 173 | S.runOnCFG(cfg); |
| 174 | } |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 175 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 176 | void LiveVariables::runOnAllBlocks(const CFG& cfg, |
| 177 | LiveVariables::ObserverTy& Obs) { |
| 178 | Solver S(*this); |
| 179 | ObserverTy* OldObserver = getAnalysisData().Observer; |
| 180 | getAnalysisData().Observer = &Obs; |
| 181 | S.runOnAllBlocks(cfg); |
| 182 | getAnalysisData().Observer = OldObserver; |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | //===----------------------------------------------------------------------===// |
| 186 | // liveness queries |
| 187 | // |
| 188 | |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 189 | bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 190 | return getBlockData(B)(D,getAnalysisData()); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 193 | bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 194 | return Live(D,getAnalysisData()); |
Ted Kremenek | e805c4a | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Ted Kremenek | e805c4a | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 197 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 198 | // printing liveness state for debugging |
| 199 | // |
| 200 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 201 | void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const { |
| 202 | const AnalysisDataTy& AD = getAnalysisData(); |
| 203 | |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 204 | for (AnalysisDataTy::decl_iterator I = AD.begin_decl(), |
| 205 | E = AD.end_decl(); I!=E; ++I) |
| 206 | if (V.getDeclBit(I->second)) { |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 207 | SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation()); |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 209 | fprintf(stderr, " %s <%s:%u:%u>\n", |
| 210 | I->first->getIdentifier()->getName(), |
| 211 | SM.getSourceName(PhysLoc), |
| 212 | SM.getLineNumber(PhysLoc), |
| 213 | SM.getColumnNumber(PhysLoc)); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 214 | } |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 217 | void LiveVariables::dumpBlockLiveness(SourceManager& M) const { |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 218 | for (BlockDataMapTy::iterator I = getBlockDataMap().begin(), |
| 219 | E = getBlockDataMap().end(); I!=E; ++I) { |
| 220 | fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n", |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 221 | I->first->getBlockID()); |
| 222 | |
| 223 | dumpLiveness(I->second,M); |
| 224 | } |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 225 | |
| 226 | fprintf(stderr,"\n"); |
| 227 | } |