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 "llvm/ADT/SmallPtrSet.h" |
| 22 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 23 | #include <string.h> |
| 24 | #include <stdio.h> |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
| 27 | |
| 28 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 29 | // Dataflow initialization logic. |
| 30 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 31 | |
| 32 | namespace { |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 33 | class RegisterDecls : public CFGRecStmtDeclVisitor<RegisterDecls> { |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 34 | LiveVariables::AnalysisDataTy& AD; |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 35 | public: |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 36 | RegisterDecls(LiveVariables::AnalysisDataTy& ad) : AD(ad) {} |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 37 | void VisitVarDecl(VarDecl* VD) { AD.Register(VD); } |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 38 | }; |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 39 | } // end anonymous namespace |
| 40 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 41 | void LiveVariables::InitializeValues(const CFG& cfg) { |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 42 | RegisterDecls R(getAnalysisData()); |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 43 | cfg.VisitBlockStmts(R); |
| 44 | } |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 45 | |
| 46 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 47 | // Transfer functions. |
| 48 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 49 | |
| 50 | namespace { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 51 | |
| 52 | static const bool Alive = true; |
| 53 | static const bool Dead = false; |
| 54 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 55 | class TransferFuncs : public CFGStmtVisitor<TransferFuncs> { |
| 56 | LiveVariables::AnalysisDataTy& AD; |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 57 | LiveVariables::ValTy LiveState; |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 58 | public: |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 59 | TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {} |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 61 | LiveVariables::ValTy& getVal() { return LiveState; } |
Ted Kremenek | 00b6327 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 63 | void VisitDeclRefExpr(DeclRefExpr* DR); |
| 64 | void VisitBinaryOperator(BinaryOperator* B); |
| 65 | void VisitAssign(BinaryOperator* B); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 66 | void VisitDeclStmt(DeclStmt* DS); |
| 67 | void VisitUnaryOperator(UnaryOperator* U); |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 68 | void VisitStmt(Stmt* S); |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 69 | void VisitExpr(Expr* E); |
| 70 | void BlockStmt_VisitExpr(Expr *E); |
| 71 | void Visit(Stmt *S); |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 72 | |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 73 | DeclRefExpr* FindDeclRef(Stmt *S); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 74 | }; |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 75 | |
| 76 | void TransferFuncs::VisitExpr(Expr * E) { |
| 77 | if (AD.getCFG().isBlkExpr(E)) return; |
| 78 | else VisitStmt(E); |
| 79 | } |
| 80 | |
| 81 | void TransferFuncs::VisitStmt(Stmt* S) { VisitChildren(S); } |
| 82 | |
| 83 | void TransferFuncs::Visit(Stmt *S) { |
| 84 | if (AD.Observer) |
| 85 | AD.Observer->ObserveStmt(S,AD,LiveState); |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 86 | |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 87 | static_cast<CFGStmtVisitor<TransferFuncs>*>(this)->Visit(S); |
| 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::VisitDeclRefExpr(DeclRefExpr* DR) { |
| 91 | if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl())) |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 92 | LiveState(V,AD) = Alive; |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 93 | } |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 94 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 95 | void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) { |
Ted Kremenek | 00b6327 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 96 | if (B->isAssignmentOp()) VisitAssign(B); |
| 97 | else VisitStmt(B); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 100 | void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) { |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 101 | Stmt *S = U->getSubExpr(); |
| 102 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 103 | switch (U->getOpcode()) { |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 104 | case UnaryOperator::PostInc: |
| 105 | case UnaryOperator::PostDec: |
| 106 | case UnaryOperator::PreInc: |
| 107 | case UnaryOperator::PreDec: |
| 108 | case UnaryOperator::AddrOf: |
| 109 | // Walk through the subexpressions, blasting through ParenExprs |
| 110 | // until we either find a DeclRefExpr or some non-DeclRefExpr |
| 111 | // expression. |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 112 | if (DeclRefExpr* DR = FindDeclRef(S)) { |
| 113 | // Treat the --/++/& operator as a kill. |
| 114 | LiveState(DR->getDecl(),AD) = Dead; |
| 115 | if (AD.Observer) { AD.Observer->ObserverKill(DR); } |
| 116 | return VisitDeclRefExpr(DR); |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 117 | } |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 118 | |
| 119 | // Fall-through. |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 120 | |
| 121 | default: |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 122 | return Visit(S); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 126 | DeclRefExpr* TransferFuncs::FindDeclRef(Stmt *S) { |
| 127 | for (;;) |
| 128 | if (ParenExpr* P = dyn_cast<ParenExpr>(S)) { |
| 129 | S = P->getSubExpr(); continue; |
| 130 | } |
| 131 | else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) |
| 132 | return DR; |
| 133 | else |
| 134 | return NULL; |
| 135 | } |
| 136 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 137 | void TransferFuncs::VisitAssign(BinaryOperator* B) { |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 138 | Stmt* LHS = B->getLHS(); |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 139 | |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 140 | // Assigning to a variable? |
| 141 | if (DeclRefExpr* DR = FindDeclRef(LHS)) { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 142 | LiveState(DR->getDecl(),AD) = Dead; |
| 143 | if (AD.Observer) { AD.Observer->ObserverKill(DR); } |
| 144 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 145 | // Handle things like +=, etc., which also generate "uses" |
| 146 | // of a variable. Do this just by visiting the subexpression. |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 147 | if (B->getOpcode() != BinaryOperator::Assign) |
| 148 | VisitDeclRefExpr(DR); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 149 | } |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 150 | else // Not assigning to a variable. Process LHS as usual. |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 151 | Visit(LHS); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 153 | Visit(B->getRHS()); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 156 | void TransferFuncs::VisitDeclStmt(DeclStmt* DS) { |
| 157 | // Declarations effectively "kill" a variable since they cannot |
| 158 | // possibly be live before they are declared. |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 159 | for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator()) |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 160 | LiveState(D,AD) = Dead; |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 161 | } |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 162 | |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 163 | void TransferFuncs::BlockStmt_VisitExpr(Expr* E) { |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 164 | assert (AD.getCFG().isBlkExpr(E)); |
Ted Kremenek | faa6383 | 2007-11-19 06:36:49 +0000 | [diff] [blame] | 165 | VisitChildren(E); |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 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 | } |