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